我正在尝试读取使用 html 页面上的输入类型文件选择的文件。我已经实现了读取文件的功能,并且可以读取文件内容。但实际问题是正在读取文件内容异步地它允许执行脚本的其他功能。我将读取的文件内容存储在数组中。
当移动到其他函数时,数组是空的。当引入延迟时,数组就有内容。有人可以帮我解决这个问题而不造成延迟吗?
我的读取文件的代码是
var getBinaryDataReader = new FileReader();
getBinaryDataReader.onload = (function(theFile) {
return function(frEvnt)
{
file[fileCnt]=frEvnt.target.result;
}
})(document.forms[formPos].elements[j].files[0]);
getBinaryDataReader.readAsBinaryString(document.forms[formPos].elements[j].files[0]);
提前致谢。
我认为您必须执行异步调用(也如 Ajax)中必须执行的操作:将稍后需要运行的代码移至读取文件时执行的回调中。
getBinaryDataReader.onload = function(theFile) {
// theFile.target.result has your binary
// you can move it into the array
// (I think you are already doing this properly)
// but then ...
nowCallTheOtherCodeThatNeedsToRunLater();
// or if you want to wait until all elements
// in the array are downloaded now
if (myArrayIsFullNow()){
callTheCodeThatNeedsTheFullArray();
}
// else: do nothing, the final file to finish downloading
// will trigger the code
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)