【问题标题】:Angular 6 download API excel fileAngular 6 下载 API excel 文件
【发布时间】:2018-08-01 22:14:56
【问题描述】:

我想在响应时下载一个 excel 文件: 这就是我所做的:

download(type: string) {
 this.http.get(`download_template?template=${type}`).subscribe(data => {});
}

这就是我得到的:

【问题讨论】:

    标签: excel download angular6


    【解决方案1】:

    做过类似的事情......这是我下面的方法,它有效。

    安装 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' })
    
    }}
    

    【讨论】:

    • 如何获取下载的excel文件名?我不想提供新名称YourFileName.xlsx
    【解决方案2】:

    在您的后端创建一个端点,在请求时返回您想要的文件。

    在你的前端这样使用

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

    【讨论】:

      猜你喜欢
      • 2019-09-01
      • 2019-02-08
      • 1970-01-01
      • 2019-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-12
      • 1970-01-01
      相关资源
      最近更新 更多