vue2下载

 1.接口请求下载(通用下载)

    async download() {
      const params = {
        fileName: this.pictureFile.fileName,
        filePath: this.pictureFile.filePath,
      }
      axios.post("/mt-ocr/api/image/downloadFile", params, { responseType: "blob" })
        .then((res) => {
          const elink = document.createElement("a");
          elink.download = params.fileName;//带后缀的文件名
          elink.style.display = "none";
          const blob = new Blob([res.data]);
          // 兼容webkix浏览器,处理webkit浏览器中href自动添加blob前缀,默认在浏览器打开而不是下载
          const URL = window.URL || window.webkitURL
          const href = URL.createObjectURL(blob);
          elink.href = href;
          document.body.appendChild(elink);
          elink.click();
          document.body.removeChild(elink);
          window.URL.revokeObjectURL(href);
        })
        .catch((err) => {
          throw new Error(err);
        });
    },

 2.封装下载方法

// 返回文件流下载
download (res, type, filename) {
  // 创建blob对象,解析流数据
  const blob = new Blob([res], {
    // 设置返回的文件类型
    // type: 'application/pdf;charset=UTF-8' 表示下载文档为pdf,如果是word则设置为msword,excel为excel
    type: type
  })
  // 这里就是创建一个a标签,等下用来模拟点击事件
  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 = filename
  document.body.appendChild(a)
  // 点击a标签,进行下载
  a.click()
  // 收尾工作,在内存中移除URL 对象
  document.body.removeChild(a)
  window.URL.revokeObjectURL(herf)
}

使用方式 

//res文件流,

 download(res, row, fileName)或

 download(res, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', fileName)

3.window.location.href的方式下载

 window.location.href = `${this.$prefix}/api/ocr/downloadResultFile?id=${this.ocrFile.id}&fileSuffix=${fileSuffix}`;

       

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值