How to read a local text file?


I’m trying to write a simple text file reader by creating a function that takes in the file’s path and converts each line of text into a char array, but it’s not working.

function readTextFile() {
  var rawFile = new XMLHttpRequest();"GET", "testing.txt", true);
  rawFile.onreadystatechange = function() {
    if (rawFile.readyState === 4) {
      var allText = rawFile.responseText;
      document.getElementById("textSection").innerHTML = allText;

What is going wrong here?

This still doesn’t seem to work after changing the code a little bit from a previous revision and now it’s giving me an XMLHttpRequest exception 101.

I’ve tested this on Firefox and it works, but in Google Chrome it just won’t work and it keeps giving me an Exception 101. How can I get this to work on not just Firefox, but also on other browsers (especially Chrome)?

9/5/2017 12:56:06 PM

Accepted Answer

You need to check for status 0 (as when loading files locally with XMLHttpRequest, you don't get a status returned because it's not from a Webserver)

function readTextFile(file)
    var rawFile = new XMLHttpRequest();"GET", file, false);
    rawFile.onreadystatechange = function ()
        if(rawFile.readyState === 4)
            if(rawFile.status === 200 || rawFile.status == 0)
                var allText = rawFile.responseText;

And specify file:// in your filename:

5/7/2014 1:51:21 AM

After the introduction of fetch api in javascript, reading file contents could not be simpler.

reading a text file

  .then(response => response.text())
  .then(text => console.log(text))
  // outputs the content of the text file

reading a json file

  .then(response => response.json())
  .then(jsonResponse => console.log(jsonResponse))     
   // outputs a javascript object from the parsed json

Update 30/07/2018 (disclaimer):

This technique works fine in Firefox, but it seems like Chrome's fetch implementation does not support file:/// URL scheme at the date of writing this update (tested in Chrome 68).

Update-2 (disclaimer):

This technique does not work with Firefox above version 68 (Jul 9, 2019) for the same (security) reason as Chrome: CORS request not HTTP. See


Visit Javascripture ! And go the section readAsText and try the example. You will be able to know how the readAsText function of FileReader works.

      var openFile = function(event) {
        var input =;

        var reader = new FileReader();
        reader.onload = function(){
          var text = reader.result;
          var node = document.getElementById('output');
          node.innerText = text;
          console.log(reader.result.substring(0, 200));
    <input type='file' accept='text/plain' onchange='openFile(event)'><br>
    <div id='output'>

var input = document.getElementById("myFile");
var output = document.getElementById("output");

input.addEventListener("change", function () {
  if (this.files && this.files[0]) {
    var myFile = this.files[0];
    var reader = new FileReader();
    reader.addEventListener('load', function (e) {
      output.textContent =;
<input type="file" id="myFile">
<textarea style="width:500px;height: 400px" id="output"></textarea>


Jon Perryman,

Yes js can read local files (see FileReader()) but not automatically: the user has to pass the file or a list of files to the script with an html <input type=file>.

Then with js it is possible to process (example view) the file or the list of files, some of their properties and the file or files content.

What js cannot do for security reasons is to access automatically (without the user input) to the filesystem of his computer.

To allow js to acccess to the local fs automatically is needed to create not an html file with js inside it but an hta document.

An hta file can contain js or vbs inside it.

But the hta executable will work on windows systems only.

This is standard browser behavior.

Also google chrome worked at the fs api, more infos here:


Provably you already try it, type "false" as follows:"GET", file, false);