我正在实现具有进度的我的应用程序中的文件上传功能,但是当我在选项中指定reportProgess: true
属性时,浏览器会取消该请求。
如果不指定上面写的属性,它将正常工作。我不明白为什么reportProgress
会发生这种情况。我也尝试过使用observe: events
,但情况相同。
取消请求后,error
中不会触发subscribe
回调,而event: undefined
会触发成功回调。
const formData: any = new FormData();
formData.append('body', this.data.module);
formData.append('fileImage', event.addedFiles[0]);
const req = new HttpRequest('POST', '/api/import/images/users', formData, {
reportProgress: true,
});
this.http.request(req).subscribe((event: HttpEvent<any>) => {
console.log({ event });
switch (event.type) {
case HttpEventType.Sent:
console.log('Request has been made!');
break;
case HttpEventType.ResponseHeader:
console.log('Response header has been received!');
break;
case HttpEventType.UploadProgress:
this.progress = Math.round(event.loaded / event.total * 100);
console.log(`Uploaded! ${this.progress}%`);
break;
case HttpEventType.Response:
console.log('User successfully created!', event.body);
break;
}
},
(e) => console.log('errrrrrrr', e)
);
我正在按照本教程https://angular.io/guide/http#report-progress并使用有角度的8.2.14
。