Knowledge Walls
John Peter
Pune, Maharashtra, India
How to read client side local file in javascript using FileReader Class with Example
11386 Views
Reading .txt file in Javascript 
FileReader class in Javascript is used to read the file in client browser. It is working in Chrome, Opera and Firefox.

FileReader returns base64 encoded text as output. using window.atob() method in javascript base64 plain/text is converted to the normal text in the below Example.
Example
<html>
    <head>
        <script type="text/javascript">
            document.getElementById("my_file_reader").onchange = function(){
                var reader = new FileReader();
                reader.onload = function(e) {
                    var data = e.target.result;
                    data = data.replace("data:text/plain;base64,","");

                    document.getElementById("data_div").innerHTML = window.atob(data);
                };
                reader.readAsDataURL(this.files[0]);
            };
        </script>
    </head>
    <body>
         <input type="file" id="my_file_reader" />
         <div id="data_div"></div>
    </body>
</html>
Demo & Output 
Best Lessons of "Good Javascript Examples"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details