前端通过Http下载后端传入的Excel(基于Angular)

17 篇文章 0 订阅
15 篇文章 0 订阅

实现思路:通过Http封装的请求方法,对参数格式进行格式匹配

首先得了解Http封装的Post方法,传入对应的三个参数url,body,options,具体代码如下:

   post(url: string, body: any | null, options: {
        headers?: HttpHeaders | {
            [header: string]: string | string[];
        };
        context?: HttpContext;
        observe?: 'body';
        params?: HttpParams | {
            [param: string]: string | number | boolean | ReadonlyArray<string | number | boolean>;
        };
        reportProgress?: boolean;
        responseType: 'blob';
        withCredentials?: boolean;
    }): Observable<Blob>;

对应的Service调用Post方法,选择文件类型{responseType: ‘blob’}:

DownLoadExcel() {
      this.http.post(
      this.baseUrl + '/prf/download_file', null, { responseType: 'blob' })
      .toPromise()
      .then((res) => {
        const blob = new Blob([res], { type: 'application/vnd.ms.excel' });  //处理返回的数据
        const file = new File([blob], 'template' + '.xlsx', {
          type: 'application/vnd.ms.excel',
        });
        saveAs(file);
      });

  }

若有其他地方使用,调用该Service中的DownLoadExcel()即可

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值