I am trying to save file array to spring boot server. One file is being saved at a time. i am iterating the file array with for loop and making a request for each file to be saved. Here i want next call to be made only after getting response for first file. I have tried the code given bellow but i am not able to achieve it.
Component.ts
public uploadNewsFiles() {
for (let index = 0; index < this.files.length; index++) {
debugger;
var body = new FormData();
// alert(this.files[index].name);
body.append('file', this.files[index]);
this.uploading.push(true);
this.uload(body,this.newsId,this.getFileDescriptionFormGroup(index)
.controls['des'].value,index).then((status:any)=>
{
if (status.status === 'success') {
// alert('success');
body.delete('file');
this.uploading[index] = false;
this.uploadingStatus.push(true);
} else {
body.delete('file');
this.uploading[index] = false;
this.uploadingStatus.push(false);
}
},(err)=>alert('failed'))
}
// body.append('newsId',this.newsId);
}
async uload(body,newsId,desc,index) {
return await this.service.saveFiles(body, newsId,desc).toPromise();
}
service.ts
public saveFiles(body,newsId,des) {
return this.http.post(this.url + '/saveFiles?
newsId='+newsId+'&fileDescription='+des,body);
}