What i've been trying to do is add the files and their matching dataurl into objects in an array.
Files is a FileList object.
I first tried this inside the onloadend
event of the Filereader but couldn't access the original file during reading so moved onto promises.
var data = [];
for(var i = 0; i < files.length; i++){
data.push({
file: files[i], //keep the files for uploading
src: readFile(files[i]) // generate the src to offer preview image
});
var last = data.length -1;
console.log(last); //log a
data[last].src.then(function(result){
console.log(last); // log b
data[last].src = result // overwrite the src deffered object with the result of the promise
});
}
readFile
is returning a deferred promise, assume this to be working.
this is working fine when files has a length of 1 but when files is multiple im having issues with the asynchronous side of things and it only working for the last item.
Result of logs based on 2 files (files.length == 2):
0 //log a
1 //log a
1 //log b <-- ignores 0 stays as 1
1 //log b
expecting 0101