Articles → JAVASCRIPT → Read Text File In Javascript Using Filereader

Read Text File In Javascript Using Filereader






Text File




Picture showing the sample text file



Read Text File




  1. Add a file upload control
  2. Add a reference of jQuery
  3. Add file upload change event
  4. Use FileReader object to read text file.


	<!DOCTYPE html>
	<html lang="en"
		xmlns="http://www.w3.org/1999/xhtml">
		<head>
			<meta charset="utf-8" />
			<title></title>
			<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
			<script>
         $(document).ready(function () {
             $("#upload").change(function (evt) {                
                 for (var i = 0, f; f = evt.target.files[i]; i++) {
                     var reader = new FileReader();
                     reader.onload = function (evt) {
                         alert(evt.target.result);
                     };
                     reader.readAsText(f);
                 }
             });
         });
      </script>
		</head>
		<body>
			<form>
				<input type="file" id="upload" />
			</form>
		</body></html>



Output




Picture showing the output of reading the text file in JavaScript using FileReader



Posted By  -  Karan Gupta
 
Posted On  -  Sunday, February 5, 2017

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250