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?

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.