1

I'm using the following code to read a local text file from a local Javascript file, but it isn't working:

var txtFile = new XMLHttpRequest();
txtFile.open('GET', fileLocation, true);

The error I get:

XMLHttpRequest cannot load file:///C:/File.txt. Cross origin requests are only supported for HTTP.

Any ideas?

sooprise
  • 22,657
  • 67
  • 188
  • 276

2 Answers2

2

You can not access local resources from javascript, You should put this file in your site and try to access it via fileLocation like http://mywebsite/File.txt

kochobay
  • 392
  • 1
  • 7
0

look at this:

var fileContent='';
var theLocation='';

function readFileViaApplet(n) {
 document.f1.t1.value='Reading in progress...';
 document.ReadURL.readFile(theLocation);
 setTimeout("showFileContent()",100);
}

function showFileContent() {
 if (document.ReadURL.finished==0) {
  setTimeout("showFileContent()",100);
  return;
 }
 fileContent=document.ReadURL.fileContent;
 document.form1.textarea1.value=fileContent;
}
Mennan
  • 4,451
  • 13
  • 54
  • 86