I'm trying to upload an array of files using FileReader which are base64 encoded and stored in an array for further processing. I'm struggling to understand the pattern I need to create in order to ensure all files have been uploaded because I have to wait for the onload event handler to fire. For example;
If I pass an array of files to the following function it will resolve the promise before the files are actually uploaded.
localUploads( event ) : any {
var response = [];
return new Promise( function(resolve, reject) {
//Retrieve all the files from the FileList object
var files = event.target.files;
var response = [];
if (files) {
for (var i=0, f; f=files[i]; i++) {
var r = new FileReader();
r.onload = (function(f) {
return function(e) {
let contents = e.target['result'];
let file = {
name: f.name,
asset: contents,
private: false
};
console.log('we are pushing into the array');
response.push( file );
};
})(f);
}
resolve( response );
}
r.readAsText(f);
});
}
Can anybody please advise a novice?
Many thanks.