2012-01-23 4 views
1

나는 로컬 자바 스크립트 파일에서 로컬 텍스트 파일을 읽을 수있는 다음 코드를 사용하고 있지만 작동하지 않습니다 :로컬 자바 스크립트 파일에서 로컬 텍스트 파일 읽기?

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

오류가 나는 얻을 :

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

어떤 아이디어?

답변

2

당신은 자바 스크립트에서 로컬 리소스에 액세스 할 수 없습니다 당신은 귀하의 사이트에서이 파일을 넣어해야하고 이것 http://mywebsite/File.txt

0

모습처럼 fileLocation 통해 액세스하려고 :

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; 
} 
관련 문제