【发布时间】:2019-06-04 22:25:21
【问题描述】:
我正在尝试将文件数组保存到 Spring Boot 服务器。一次保存一个文件。我正在使用 for 循环迭代文件数组并请求保存每个文件。在这里,我希望仅在获得第一个文件的响应后才进行下一次调用。我已经尝试了下面给出的代码,但我无法实现它。
组件.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);
}
【问题讨论】:
标签: angular asynchronous async-await angular6