【发布时间】:2020-05-11 15:36:27
【问题描述】:
我想使用 Angular 下载生成的 xls 文件,并根据响应头 Content-Disposition 设置文件名。
我使用类似的东西
downloadFile(): Observable<any> {
var url= "http://somehting";
return this.http.get(url, { observe: 'response', responseType: 'blob' as 'json' });
}
后来在我的控制器中:
this.dataService.downloadFile().subscribe(
data => {
this.debug.msg("response:", JSON.stringify(data));
saveAs(data.body, "test.xlsx");
},
err => {
console.error(err);
alert("Problem while downloading the file.\n" + "[" + err.status + "] " + err.statusText);
}
);
很遗憾,响应头没有设置,正文也是空的。
response: {
"headers":{
"normalizedNames":{
},
"lazyUpdate":null
},
"status":200,
"statusText":"OK",
"url":"http://localhost:4200/MyEndpoint/GetDownload",
"ok":true,
"type":4,
"body":{
}
}
如果我将程序更改为 responseType: blob,那么我可以获取文件的内容,但我不知道如何到达 response.headers。
我错过了什么吗?如果是这样,那是什么?
【问题讨论】: