vue 导出后端返回的二进制流文件

1:导出按钮

<el-button type="primary"  size="small" @click="onExport"><el-icon><Download /></el-icon>导出Excel</el-button>

2:js

onExport(){
      const loading = ElLoading.service({
        lock: true,
        text: '导出中,请稍后!'
      })
      const data = {
        //参数
      }
      this.$axios({
        url:'http://*****',
        method:"post",
        data:filterParams(data),
        responseType: 'blob',
        headers: {
          'X-Access-Token': localStorage.getItem('token'),
        }
      }).then(res=>{
          // 转化为blob对象
          let blob = new Blob([res.data], {
            type: "application/octet-stream",
          });
          let filename = '登记记录' + '.xls'
          // 将blob对象转为一个URL
          var blobURL = window.URL.createObjectURL(blob);
          // 创建一个a标签
          var tempLink = document.createElement("a");
          // 隐藏a标签
          tempLink.style.display = "none";
          // 设置a标签的href属性为blob对象转化的URL
          tempLink.href = blobURL;
          // 给a标签添加下载属性
          tempLink.setAttribute("download", filename);
          if (typeof tempLink.download === "undefined") {
            tempLink.setAttribute("target", "_blank");
          }
          // 将a标签添加到body当中
          document.body.appendChild(tempLink);
          // 启动下载
          tempLink.click();
          // 下载完毕删除a标签
          document.body.removeChild(tempLink);
          window.URL.revokeObjectURL(blobURL);
          this.$message({
            message: "导出成功~",
            type: "success",
          });
        loading.close()
      })
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值