Reading a text file using FileReader Javascript object for input file field - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Input Text

Description

Reading a text file using FileReader Javascript object for input file field

Demo Code

ResultView the demo in separate window

<html>
   <head></head>
   <body> 
      <input type="file" id="openfile"> 
      <br> 
      <pre id="filecontents"></pre> 
      <script type="text/javascript">
    document.getElementById("openfile").addEventListener('change', function() {
      var fr = new FileReader();/*from   w  ww  . j  av  a  2 s.com*/
      fr.onload = function() {
        document.getElementById("filecontents").textContent = this.result;
      }
      fr.readAsText(this.files[0]);
    })
  
      </script>  
   </body>
</html>

Related Tutorials