【发布时间】:2018-01-22 08:41:57
【问题描述】:
我正在尝试使用 angular 和 php 作为后端下载 excel 文件 (.xls)
我的后端已经发送了我的 excel 文件作为响应,当我检查它时,它以正确的格式返回
但我的 angular 4 将文件作为损坏的格式返回(它包含一些符号,如 ��ࡱ�;�� )
下面是我的角码:
服务
private headers = new Headers({
'Content-Type': 'application/x-www-form-urlencoded'
});
downloadTemplate() {
this.options = new RequestOptions();
this.options.headers = this.headers;
const params: URLSearchParams = new URLSearchParams();
params.set('token', this.currentUser.token);
this.options.search = params;
return this.http.get(environment.api_host + '/template', this.options);
}
组件
template() {
this._apiService.downloadTemplate().subscribe((response) => {
const blob = new Blob([(<any>response)._body], {type: 'application/vnd.ms-excel'});
const link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = 'template.xls';
document.body.appendChild(link);
link.click();
});
}
从我的代码中,有什么我想念的吗?
【问题讨论】: