我有一个页面,用户可以在其中选择一个文件夹来上传文件。在发送文件之前,我需要阅读它们并检查数据。我的代码组织如下:
$( '#folder-select' ).on('change', getValidFileList);
var fileList = [];
var getValidFileList = function(event) {
//Get the selected files
files = $( this ).get(0).files;
for(var i=0; i<files.length; i++) {
checkFile(files[i]);
}
//Do something with the final fileList
console.log(fileList);
};
var checkFile = function(file) {
var reader = new FileReader();
reader.onload = function (event) {
//Here I parse and check the data and if valid append it to fileList
};
reader.readAsArrayBuffer(file);
};
Run Code Online (Sandbox Code Playgroud)
我想使用生成的 fileList 数组来继续处理/显示上传的文件。我发现 reader.onload() 是异步调用的,所以console.log(fileList)afterfor循环的结果是一个空数组(它在reader.onload() …