2

I want to read a local text file through javascript. I copied the code from How to read a local text file?

but its not working.

Here's the code snippet,

function readTextFile(file)
{
    var rawFile = new XMLHttpRequest();
    rawFile.open("GET", file, false);

    rawFile.onreadystatechange = function ()
    {
        if(rawFile.readyState === 4)
        {
            if(rawFile.status === 200 || rawFile.status == 0)
            {
                var allText = rawFile.responseText;
                alert(allText);
            }
        }
    }
    console.log(rawFile);
     rawFile.send(null);
}

readTextFile("file:///Users/vivek/Desktop/file1.txt");

I tried printing the object(rawFile) and this is the output I got,

[object XMLHttpRequest] {
  abort: function abort() { [native code] },
  addEventListener: function addEventListener() { [native code] },
  dispatchEvent: function dispatchEvent() { [native code] },
  DONE: 4,
  getAllResponseHeaders: function getAllResponseHeaders() { [native code] },
  getResponseHeader: function getResponseHeader() { [native code] },
  HEADERS_RECEIVED: 2,
  LOADING: 3,
  onabort: null,
  onerror: null,
  onload: null,
  onloadend: null,
  onloadstart: null,
  onprogress: null,
  onreadystatechange: function ()
    {
        if(rawFile.readyState === 4)
        {
            if(rawFile.status === 200 || rawFile.status == 0)
            {
                var allText = rawFile.responseText;
                alert(allText);
            }
        }
    },
  ontimeout: null,
  open: function open() { [native code] },
  OPENED: 1,
  overrideMimeType: function overrideMimeType() { [native code] },
  readyState: 1,
  removeEventListener: function removeEventListener() { [native code] },
  response: "",
  responseText: "",
  responseType: "",
  responseURL: "",
  responseXML: null,
  send: function send() { [native code] },
  setRequestHeader: function setRequestHeader() { [native code] },
  status: 0,
  statusText: "",
  timeout: 0,
  UNSENT: 0,
  upload: [object XMLHttpRequestUpload] {
    addEventListener: function addEventListener() { [native code] },
    dispatchEvent: function dispatchEvent() { [native code] },
    onabort: null,
    onerror: null,
    onload: null,
    onloadend: null,
    onloadstart: null,
    onprogress: null,
    ontimeout: null,
    removeEventListener: function removeEventListener() { [native code] }
  },
  withCredentials: false
}

readyState is giving a value of 1, so I tried changing the condition if(rawFile.readyState === 4) to if(rawFile.readyState === 1), but still no luck.

This is the error, I am getting on JSBin,

"error"
"Error: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'file:///Users/vivek/Desktop/file1.txt'.
    at readTextFile (gazoqoq.js:18:14)
    at gazoqoq.js:21:1
    at https://static.jsbin.com/js/prod/runner-4.1.4.min.js:1:13924
    at https://static.jsbin.com/js/prod/runner-4.1.4.min.js:1:10866"
Frosted Cupcake
  • 1,909
  • 2
  • 20
  • 42

0 Answers0