【发布时间】:2018-04-03 08:37:56
【问题描述】:
我想换成新的HttpClient。到目前为止,我处理以下文件下载:
getXlsx (): Observable<any> {
return this.http.get('api/xlsx', {
responseType: ResponseContentType.ArrayBuffer, // set as ArrayBuffer instead of Json
})
.map(res => downloadFile(res, 'application/xlsx', 'export.xlsx'))
.catch(err => handleError(err));
}
export function downloadFile(data: any, type: string, filename: string): string {
const blob = new Blob([data._body], { type });
const url = window.URL.createObjectURL(blob);
// create hidden dom element (so it works in all browsers)
const a = document.createElement('a');
a.setAttribute('style', 'display:none;');
document.body.appendChild(a);
// create file, attach to hidden element and open hidden element
a.href = url;
a.download = filename;
a.click();
return url;
}
将 respondeType 更改为 'arraybuffer' 将导致文件为空。任何想法如何解决它?
【问题讨论】:
-
您是否也尝试过使用
responseType: 'blob'+ 删除new Blob()? -
非常感谢!它正在工作!
标签: angular blob httpclient arraybuffer