vue下载文件--excel文件及pdf文件及zip压缩包

写下载功能前,请求接口中需要加responseType及headers

export function zipDownload(query) {
  return request({
    url: baseUrl + '/download',
    method: 'get',
    params: query,
    responseType: "blob",
    headers: { "Content-Type": "multipart/form-data" }
  })
}

一、下载excel

 downloadExcel() {
      ExcelDownload({ fileName: this.excelname})
        .then((response) => {
          let url = window.URL.createObjectURL(
            new Blob([response], {
              type: "application/vnd.ms-exce",  //此处type设置vnd.ms-exce
            })
          );
          let link = document.createElement("a");
          link.style.display = "none";
          link.href = url;
          link.setAttribute(
            "download",
            "下载文件.xlsx'    //此处需注意 加.xlsx
          );
          document.body.appendChild(link);
          link.click();
        })
        .catch((err) => {
          console.log(err);
        });
    },

二、下载zip压缩包

  daochu() {
     
      zipDownload({ fileName: this.zipname})
        .then((response) => {
          let url = window.URL.createObjectURL(
            new Blob([response], {
              type: "application/zip",    //此处type是zip
            })
          );
          let link = document.createElement("a");
          link.style.display = "none";
          link.href = url;
          link.setAttribute(
            "download",
            "下载的包"
          );
          document.body.appendChild(link);
          link.click();
        })
        .catch((err) => {
          console.log(err);
        });

三、下载pdf

 downPdf() {
      const query = { fileName: this.fileName };
      downPDF(query)
        .then((res) => {
          
          const blob = new Blob([res], {
            type: "application/pdf;chartset=UTF-8",   //此处type是pdf
          });
          const a = document.createElement("a");
          const URL = window.URL || window.webkitURL;
          const herf = URL.createObjectURL(blob);
          a.href = herf;
          a.download = this.fileName;
          document.body.appendChild(a);
          a.click();
          document.body.removeChild(a);
          window.URL.revokeObjectURL(herf);
        })
        .catch((err) => {
          // 创建blob对象,解析流数据
          const blob = new Blob([err], {
            // 如何后端没返回下载文件类型,则需要手动设置:type: 'application/pdf;chartset=UTF-8' 表示下载文档为pdf,如果是word则设置为msword,excel为excel
            type: "application/pdf;chartset=UTF-8",
          });
          const a = document.createElement("a");
          // 兼容webkix浏览器,处理webkit浏览器中href自动添加blob前缀,默认在浏览器打开而不是下载
          const URL = window.URL || window.webkitURL;
          // 根据解析后的blob对象创建URL 对象
          const herf = URL.createObjectURL(blob);
          // 下载链接
          a.href = herf;
          // 下载文件名,如果后端没有返回,可以自己写a.download = '文件.pdf'
          a.download = this.fileName;
          document.body.appendChild(a);
          a.click();
          document.body.removeChild(a);
          // 在内存中移除URL 对象
          window.URL.revokeObjectURL(herf);
        });
    },

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值