i am using fileReader for uploading images, when images are done uploading i want to send them to server.
my problem is that when there are more than 1 image it sends multiple requests, because i have for loop. how can i make it work?
for (let file of selectedFiles) {
const fileName = file.name;
const reader = new FileReader();
reader.readAsArrayBuffer(file);
reader.onload = () => {
const image = reader.result as any;
var base64 = btoa(
new Uint8Array(image)
.reduce((data, byte) => data + String.fromCharCode(byte), '')
);
imageArray.push({ fileName: fileName, content: base64 });
console.log(imageArray);
this.service.create(this.finalData).subscribe({
next: data => {
console.log(data);
this.openSnackBar("Success", 'x');
},
error: error => {
console.error('There was an error!', error);
}
})
}
}