I'm trying to read and add multiple files to an array. I've already found out that readAsArrayBuffer is a asynchronous function, so i need to wait for previous uploads to end. I tried to use callbacks but i failed.
<form enctype="multipart/form-data">
<input id="file-input" type="file" multiple="" accept="image/*">
</form>
<div id="upload-list"></div>
Here's js file:
var fileList = [];
$(document).ready(function() {
function addFiles(files) {
for (var i = 0; i < files.length; i++) {
var file = files[i];
var reader = new FileReader();
reader.onloadend = (function(file) {
return function() {
fileList.push(reader.result);
$('#upload-list').append('<div class="upload-list-item">' + file.name '</div>');
}
})(file);
reader.readAsArrayBuffer(file);
console.log(fileList);
}
}
$('#file-input').on('change', function(e) {
addFiles(e.target.files);
});
}
So now i have new FileReader each iteration, i got rid from Failed to execute 'readAsArrayBuffer' on 'FileReader': The object is already busy reading Blobs error, now i have the same files in my array, although I selected different.

fileList.push(reader.result);... the problem is whatreaderwill be (you fixedfileusing an IIFE, but notreader