【发布时间】:2018-08-01 22:14:56
【问题描述】:
我想在响应时下载一个 excel 文件: 这就是我所做的:
download(type: string) {
this.http.get(`download_template?template=${type}`).subscribe(data => {});
}
这就是我得到的:
【问题讨论】:
我想在响应时下载一个 excel 文件: 这就是我所做的:
download(type: string) {
this.http.get(`download_template?template=${type}`).subscribe(data => {});
}
这就是我得到的:
【问题讨论】:
做过类似的事情......这是我下面的方法,它有效。
安装 npm i --save file-saver
服务
download(type: string) {
return this.http.get(`download_template?template=${type}`, {responseType: 'blob'});
}
组件
...
import { saveAs } from 'file-saver';
...
this._service.download(type: string).subscribe(res => {
saveAs(res, 'YourFileName.xlsx',
{ type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
}}
【讨论】:
YourFileName.xlsx。
在您的后端创建一个端点,在请求时返回您想要的文件。
在你的前端这样使用
<a [href]=URLoftheFile download="FilenameWithExtension">DownloadFile</a>
【讨论】: