【发布时间】:2020-02-08 15:50:24
【问题描述】:
我有一个返回 excel 文档作为响应的 API。请求将是一个简单的 json。
我搜索了谷歌并找到了一些用于下载文件的代码库,并且我已经在我的应用程序中使用了它,但是我遇到了一些错误并且无法找到解决方案。以下是我的代码库。
component.ts
import rxjs/Rx;
details = {id: 75,name: "some name"}
this.nameService.getData(this.details).subscribe(response => {
this.downloadFile(response);
})
downloadFile(data: Response) {
const blob = new Blob([data], { type: '.xlsx' });
const url= window.URL.createObjectURL(blob);
window.open(url);
}
nameService.ts
getData(postData) {
return this.httpService.post('https://localhost:8080/getFile/', postData);
}
httpService.ts
constructor(private http: HttpClient)
post(apiEndpoint: string, request: any) :Observable<any> {
return this.http.post<any>(apiEndpoint,request);
}
使用上面的代码库,我遇到两个错误并且文件没有下载。
- 得到错误:
“类型响应不可分配给类型 Blobpart”在创建 Blob(const blob = new Blob([data], { type: '.xlsx' });)
- 如果我将数据更改为任何
(downloadFile(data: any)),上述错误(1) 将会消失,但我收到httperror响应为“语法错误:json 中位于json.parse位置0 的意外标记P
如果有人找到上述问题的任何解决方案,请提供帮助。
【问题讨论】: