解决:
查找官网,使用nzCustomRequest方法自定义自己的上传实现
获取 HttpErrorResponse 返回 onError 状态
完整代码
customReq = (item: UploadXHRArgs) => {
// Create a FormData here to store files and other parameters.
const formData = new FormData();
// tslint:disable-next-line:no-any
formData.append('file', item.file as any);
formData.append('id', '1000');
const req = new HttpRequest('POST', item.action!, formData, {
reportProgress: true,
withCredentials: true
});
// Always returns a `Subscription` object. nz-upload would automatically unsubscribe it at correct time.
return this.http.request(req).subscribe(
// tslint:disable-next-line no-any
(event: HttpEvent<any>) => {
if (event.type === HttpEventType.UploadProgress) {
if (event.total! > 0) {
// tslint:disable-next-line:no-any
(event as any).percent = (event.loaded / event.total!) * 100;
}
item.onProgress!(event, item.file!);
} else if (event instanceof HttpResponse) {
item.onSuccess!(event.body, item.file!, event);
} else if (event instanceof HttpErrorResponse){
item.onError!(event.error, item.file!);
}
},
err => {
item.onError!(err, item.file!);
}
);
}