我试图下载在API响应中作为 blob 收到的 XLSX 文件,但该文件以 HttpErrorResponse
这是由于angular5的角度请求结构发生了变化。
我尝试了如下请求,该请求通过HttpErrorReponse对象响应:
myBlobRequest():Observable<any>{
return this.http.get(url, { responseType: 'blob'})
.map((response: Response) => {
return response;
})
.catch((res: Response) => {
// handleError(response)
});
}
答案 0 :(得分:-1)
以下解决方案对我有用。由于它在 HttpErrorResponse
中提供了json解析错误解决方案:
{ responseType: 'blob'} should be replaced with { responseType: 'blob' as 'json'}
请求:
获取方法:
myBlobRequest():Observable<any>{
return this.http.get(url, { responseType: 'blob' as 'json' })
.map((response: Response) => {
return response;
})
.catch((res: Response) => {
// handleError(response)
});
}
响应:
处理订户功能:
this.serviceObj.myBlobRequest.subscribe((blobResponse)=>{
var blob = new Blob([resp], { type: 'application/vnd.openxmlformats-
officedocument.spreadsheetml.sheet;' });
FileSaver.saveAs(blob, "test.xlsx");
});
this.serviceObj.myBlobRequest.subscribe((blobResponse)=>{
var blob = new Blob([resp], { type: 'application/vnd.openxmlformats-
officedocument.spreadsheetml.sheet;' });
// Download File without FileSaver
const downloadUrl = window.URL.createObjectURL(blob);
var link = document.createElement('a');
link.href = downloadUrl;
link.download = "test.xlsx";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
});