[英]How to download an excel file in Angular 8 as an API response
[英]Angular 6 download API excel file
做了類似的事情......這是我的方法,它起作用。
安裝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.