파일에서 텍스트를 읽고 함수로 반환하고 싶습니다. 그래서 여기 내 코드의 중요한 부분이다 :파일 로딩 대기 (자바 스크립트 onload)
function getFileRequest(id, contentType, callback) {
var val = "x";
if (window.File && window.FileReader && window.FileList && window.Blob) {
var element = document.getElementById(id);
var file = element.files[0];
if(file != null) {
if(file.type.match("text/xml")){
var r;
r = new FileReader();
r.onload = function (e) {
val = e.target.result;
}
r.readAsText(file);
}
else
alert("Wrong file format");
}
} else {
alert('The File APIs are not fully supported by your browser.');
}
alert(val);
if(val == null)
return "";
else
return getRequestBody(id,contentType,val);
}
내가 "발"이라는 변수에 텍스트를 전달하려는. 하지만 최소한 내게 보이는 것처럼, alert (val)는 항상 onload 함수가 실행되기를 기다리지 않기 때문에 항상 기본 "x"를 표시합니다. 내가 맞습니까? 어떻게 그 텍스트에 액세스 할 수 있습니까? 예외를 기다릴 수있는 방법이 있습니까?
당신은 동기와 같은 비동기 작업을 처리 할 수 . – epascarello