Angular 从内容处置中下载名为的角度文件
我想使用angular下载生成的xls文件,并根据响应头内容配置设置文件名 我用的是Angular 从内容处置中下载名为的角度文件,angular,angular-material,Angular,Angular Material,我想使用angular下载生成的xls文件,并根据响应头内容配置设置文件名 我用的是 downloadFile(): Observable<any> { var url= "http://somehting"; return this.http.get(url, { observe: 'response', responseType: 'blob' as 'json' }); } 不幸的是,没有设置响应头,主体也是空的 response: { "headers":
downloadFile(): Observable<any> {
var url= "http://somehting";
return this.http.get(url, { observe: 'response', responseType: 'blob' as 'json' });
}
不幸的是,没有设置响应头,主体也是空的
response: {
"headers":{
"normalizedNames":{
},
"lazyUpdate":null
},
"status":200,
"statusText":"OK",
"url":"http://localhost:4200/MyEndpoint/GetDownload",
"ok":true,
"type":4,
"body":{
}
}
如果我将过程更改为responseType:blob,那么我可以获取文件的内容,但我不知道如何获取response.headers。
我错过了什么吗?如果是这样的话,是什么 基于其他堆栈溢出帖子……这对我来说很有用 在服务器集头上
Response.Headers.Add("Access-Control-Expose-Headers", "content-disposition");
下载过程的角度数据服务定义。重要的是将observable设置为HttpResponse
downloadFile(): Observable<HttpResponse<Blob>> {
var url = "http://host/GetDownload";
return this.http.get<Blob>(url, { observe: 'response', responseType: 'blob' as 'json' });
}
downloadFile(): Observable<HttpResponse<Blob>> {
var url = "http://host/GetDownload";
return this.http.get<Blob>(url, { observe: 'response', responseType: 'blob' as 'json' });
}
this.dataService.downloadFile().subscribe(
data => {
var fileName = "report.xlsx";
const contentDisposition = data.headers.get('Content-Disposition');
if (contentDisposition) {
const fileNameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
const matches = fileNameRegex.exec(contentDisposition);
if (matches != null && matches[1]) {
fileName = matches[1].replace(/['"]/g, '');
}
}
saveAs(data.body, fileName);
},
err => {
console.error(err);
this.blockUI.stop();
alert("Problem while downloading the file.\n"+
"["+err.status+"] "+err.statusText);
});
}