Do you have any suggestions on how to improve my fileReader for uploading images? I am currently facing an issue where multiple requests are being sent to the server when there are more than 1 image due to a for loop. How can I modify my code to address this issue?
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);
}
})
}
}