Whenever I attempt to upload a large number of files simultaneously, I encounter an issue.
The API interface only allows for the submission of one file at a time, requiring me to call the service for each individual file. Currently, my code looks like this:
onFilePaymentSelect(event): void {
if (event.target.files.length > 0) {
this.paymentFiles = event.target.files[0];
}
let i = 0;
let save = 0;
const numFiles = event.target.files.length;
let processed = 0;
if (event.target.files.length > 0) {
while (event.target.files[i]) {
const formData = new FormData();
formData.append('file', event.target.files[i]);
this.payrollsService.sendFilesPaymentName(formData).subscribe(
(response) => {
let added = null;
processed++;
if (response.status_message === 'File saved') {
added = true;
save++;
} else {
added = false;
}
this.payList.push({ filename, message, added });
});
i++;
}
}
Essentially, I am using a loop to sequentially send each file to the API, but when handling a large volume of files, I receive a "429 too many request" error. Is there any way to optimize this process?