HTML 5 provides a standard way to interact with local files with the help of File API. The File API allows interaction with single, multiple as well as BLOB files. The FileReader API can be used to read a file asynchronously in collaboration with JavaScript event handling. However, all the browsers do not have HTML 5 support so it is important to test the browser compatibility before using the File API. There are four inbuilt methods in the FileReader API to read local files:
- FileReader.readAsArrayBuffer(): Reads the contents of the specified input file. The result attribute contains an ArrayBuffer representing the file’s data.
- FileReader.readAsBinaryString(): Reads the contents of the specified input file. The result attribute contains the raw binary data from the file as a string.
- FileReader.readAsDataURL(): Reads the contents of the specified input file. The result attribute contains a URL representing the file’s data.
- FileReader.readAsText(): Reads the contents of the specified input file. The result attribute contains the contents of the file as a text string. This method can take encoding version as the second argument(if required). The default encoding is UTF-8.
In this case we are using FileReader.readAsText() method to read local .txt file.
<!DOCTYPE html> <html> <head> <title>Read Text File</title> </head> <body> <input type="file" name="inputfile" id="inputfile"> <br> <pre id="output"></pre> <script type="text/javascript"> document.getElementById('inputfile') .addEventListener('change', function() { var fr=new FileReader(); fr.onload=function(){ document.getElementById('output') .textContent=fr.result; } fr.readAsText(this.files[0]); }) </script> </body> </html> |

This code prints the content of the input file exactly the same as is there in the input file.
Recommended Posts:
- Javascript | Program to read text File
- Local File Inclusion (LFI)
- How to include a JavaScript file in another JavaScript file ?
- How to read and write JSON file using Node.js ?
- How to read a file line by line using node.js ?
- How to select all text in HTML text input when clicked using JavaScript?
- How to convert UTC date time into local date time using JavaScript ?
- Remove the Chrome's "No file chosen option" from a file input using JavaScript
- How to Read a File Line by Line to String in Golang?
- How to find record using any key-value pair information of record in your local/custom database using Node.js ?
- Global and Local variables in JavaScript
- JavaScript | Program to write data in a text File
- How to load the contents of a text file into a JavaScript variable?
- JavaScript | WebAPI | File | File.type Property
- JavaScript | WebAPI | File | File.size Property
- JavaScript | WebAPI | File | File.name Property
- Text Animation with changing the color of the text using HTML & CSS
- How to create long shadow of text without using text-shadow in HTML and CSS ?
- Nodejs | authentication using Passportjs and passport-local-mongoose
- How to Get Local IP Address of System using PHP ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


