html 5 파일 판독기를 사용하여 두 개의 텍스트 파일을 업로드하려고합니다. 배열에 파일을 넣을 수는 있지만 함수에서 배열을 반환 할 수는 없습니다. 나는 그것들을 글로벌 배열로 밀어 넣고 그걸로 작업 할 수 있다고 생각합니다. 그러나이 질문을하는 것에 대한 나의 주된 이유는 여기서 분명히 이해할 수없는 것이 있기 때문에 왜 수익이 효과가 없는지를 배우는 것입니다.html5 파일 판독기를 사용하여 업로드 한 여러 파일에서 텍스트를 푸시하는 이유는 무엇입니까?
function processFiles(files) {
console.log(getEmails(files)); //this statement should log textArray to console, but returns undefined instead
function getEmails(files){
var textArray = [];
for(var i=0; i<files.length; i++){
(function(files,i){
var file = files[i];
var reader = new FileReader();
reader.onload = function (e) {
textArray.push(e.target.result);
if (i==(files.length-1))
{
console.log(textArray); //this logs test array to the console no probs
return textArray;// so why wont this return text array so the previous console.log works
}
};
reader.readAsText(file,"Unicode");
})(files,i);
}
}
}
<input id="fileInput" type="file" onchange="processFiles(this.files)" multiple>