我在Google上搜索并找到了一些代码库来下载文件,并在我的应用程序中使用了它,但是我遇到了一些错误,无法找到解决办法。以下是我的代码基础:
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);
}
使用上述代码库时,我遇到了以下两个错误,并且文件无法下载。
- 错误信息如下:
在创建 Blob(const blob = new Blob([data], { type: '.xlsx' });)时出现“类型 Response 无法分配给类型 Blobpart”的错误。
- 如果我将数据更改为任意类型
(downloadFile(data: any))
,上述错误(1)将消失,但我会得到一个httperror
响应,内容为“语法错误:在 json 的位置 0 处出现意外的标记 P,位于json.parse
”。
如果有人找到了解决上述问题的方法,请帮忙提供帮助。