2012-01-23 251 views
1

我使用下面的代碼來讀取本地JavaScript文件本地文本文件,但它不工作:從本地JavaScript文件中讀取本地文本文件?

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

的錯誤,我得到:

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

任何想法?

回答

0

看看這個:

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