簡體   English   中英

Angular 6下載API excel文件

[英]Angular 6 download API excel file

我想在響應時下載一個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' })

}}

在您的后端創建一個端點,在請求時返回您想要的文件。

在你的前端以這種方式使用

<a [href]=URLoftheFile download="FilenameWithExtension">DownloadFile</a>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM