我正在做类似于http://www.html5rocks.com/en/tutorials/file/dndfiles/
我正在做的是我一次读取一个选定文件的内容,以验证它们的行是否通过了一些正则表达式测试。完成验证所有文件后,我需要相应地更新(启用/禁用)一些按钮,因此回调功能
是否有可能有一个回调函数在读取所有内容后执行某些操作?
HTML:
<input type="file" id="files" name="files[]" multiple />
Javascipt:
<script>
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
// files is a FileList of File objects. List some properties.
var validArray = [];
for (var i = 0, f; f = files[i]; i++) {
//Create new file reader
var r = new FileReader();
//On load call
r.onload = (function (f) {
return function (e) {
var contents = e.target.result;
var lines = contents.split('\n');
for(var i=0; i<lines.length; i++){
//Validate regex of line here
//If line does not pass, append file name to validArray and break
}
};
})(f);
r.readAsText(f);
}
}
document.getElementById('files').addEventListener('change', handleFileSelect, false);
</script>