0

I want to read a remote text file using FileReader in javascript. I'm able to read a file which is chosen from input file, like:

HTML:

<input type="file" id="file-input" />
<button onclick="upload()">Click me</button>  

JS:

function upload() {    
      var textFile = document.getElementById('file-input').files[0];
      var fileReader = new FileReader();
      fileReader.readAsText(textFile);
      fileReader.onloadend = function (evt) {
        if (evt.target.readyState === fileReader.DONE) { // DONE == 2
          console.log("File content:" + evt.target.result);
        }
      };
}

But as said, I don't want to read a file which is chosen from input but I want to fetch it from a URL like www.example.com/abc.txt

Any help would be appreciated.

Thanks

Parth Vora
  • 4,073
  • 7
  • 36
  • 59
  • *Same origin Policy* means this is only possible using Ajax, and only if the remote file is hosted on the same domain as the page requesting it or the remote domain actively allows you to do this (it probably won't) See http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy – Alex K. Feb 10 '16 at 15:40
  • Yes, remote file is hosted on the same domain as the page requesting – Parth Vora Feb 11 '16 at 07:01
  • Then you would use Ajax to load the data into a string – Alex K. Feb 11 '16 at 10:10

0 Answers0