Articles → JAVASCRIPT → Read text file in JavaScript using FileReader

Read text file in JavaScript using FileReader






Text file




Picture showing the sample text file

Click to Enlarge


Read text file














<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
   <head>
      <meta charset="utf-8" />
      <title></title>
      <script src="jquery-3.1.1.js"></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

Click to Enlarge


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

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250