提问人:Илья Никитин 提问时间:11/6/2023 更新时间:11/6/2023 访问量:35
Angular:接收文件的 HTTP 请求中的错误处理
Angular: Error handling in HTTP request to receive a file
问:
我需要向服务器发送请求,如果出现问题,我希望服务器以文件或错误消息进行响应。问题是我正在努力设置我的请求以处理文件和错误。我怎样才能做到这一点?
按需提供服务
export(id: string, poolsName: string[]): Observable<Blob> {
return this.http.post<Blob | any>(`${environment.apiUrl}/item/${id}/export`, poolsName)
}
元件
async fullExport(poolsName: string[]){
if(this.projectId) {
this.projectsService.export(this.projectId, poolsName).subscribe(
(response: any) => {
console.log("res: ", response);
// const blob = new Blob([response], { type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation' });
// const url = window.URL.createObjectURL(blob);
// const a = document.createElement('a');
// document.body.appendChild(a);
// console.log(url);
// a.style.display = 'none';
// a.href = url;
// a.download = 'output.pptx';
// a.click();
},
(error: any) => {
console.log("error: ", error);
}
);
}
}
执行请求时,如果它返回一个文件,我会在浏览器控制台中收到此错误
{
"headers": {
"normalizedNames": {},
"lazyUpdate": null
},
"status": 201,
"statusText": "Created",
"url": "http://localhost:8080/item/6310716b78015e32433ee34d/export",
"ok": false,
"name": "HttpErrorResponse",
"message": "Http failure during parsing for http://localhost:8080/item/6310716b78015e32433ee34d/export",
"error": {
"error": {},
"text": "PK\u0003\u0004\u0014\u0000\b\b\b\u0000�..."
}
}
我尝试将 { responseType: 'blob' } 添加到 http 请求中,然后正常接收文件,但如果发生错误,我无法接收。
答: 暂无答案
评论