【发布时间】:2020-02-14 19:19:25
【问题描述】:
我正在尝试从后端下载具有特定文件名的 .csv 文件。如何使用 Angular 7 从 api 响应标头访问文件名
我的服务(api 调用):
public getDownloadFile(masterDistributorId: number, runDate: string) {
return this.http.get
(`${environment.apiUrl}command-center/report/download/?
masterDistributorId=${masterDistributorId}&runDate=${runDate}`, {
headers: new HttpHeaders().append('Content-Type', 'application/csv'),
responseType: 'blob',
observe: 'response'
}
);
}
我的订阅电话:
public Download(row: DistributorItem) {
console.log(row.masterDistributorId);
console.log(this.RunDate);
this.CommandService.getDownloadFile(row.masterDistributorId, this.RunDate).subscribe(res => {
console.log(res.headers);
this.SaveFile(res);
});
}
【问题讨论】:
-
showConfigResponse() { this.configService.getConfigResponse() // resp 的类型为
HttpResponse<Config>.subscribe(resp => { // 显示其标题 const keys = resp.headers.keys() ; this.headers = keys.map(key =>${key}: ${resp.headers.get(key)}); // 直接访问body,类型为Config.this.config = { ... resp.body }; }); } 访问angular.io/guide/http了解更多详情。
标签: angular