vue处理后台返回流的形式的文件下载

get请求
var url=`http://172.16.0.35:8082/report/down/${val.evaluateId}/${val.type}`;
window.open(url);
post请求=>将该文件打包为.zip的压缩包并导出
kalman_btn(val) {
      var that=this;
      axios({
        headers: {
          'Content-Type':'application/json'
        },
        method: 'post',
        url: 'http://172.16.0.35:8082/report/down',
        data: val,
        responseType: 'blob'
      }).then(response => {
        that.download(response.data)
      }).catch((error) => {

      })
    },
    download (data) {
      const blob = new Blob([data]);//处理文档流
      const fileName = 'excel.xlsx';
      const elink = document.createElement('a');
      elink.download = fileName;
      elink.style.display = 'none';
      elink.href = URL.createObjectURL(blob);
      document.body.appendChild(elink);
      elink.click();
      URL.revokeObjectURL(elink.href); // 释放URL 对象
      document.body.removeChild(elink);
    },
post请求=>导出该文件
axios({
          headers: {
            "Content-Type": "application/json",
          },
          method: "get",
          url: BASEURL + "/web/pipe/qr/code/export",
          params: { token },
          responseType: "blob",
        })
          .then((res) => {
            // that.download(res.params);
            // console.log('res',res);
            
            const fileName = res.headers["content-disposition"].split("=")[1];
            const _res = res.data;
            that.download2(_res,fileName);
          })
          .catch((error) => {});
download2(_res,fileName){
      let blob = new Blob([_res]);
      let downloadElement = document.createElement("a");
      let href = window.URL.createObjectURL(blob); //创建下载的链接
      downloadElement.href = href;
      downloadElement.download = fileName; //下载后文件名
      document.body.appendChild(downloadElement);
      downloadElement.click(); //点击下载
      document.body.removeChild(downloadElement); //下载完成移除元素
      window.URL.revokeObjectURL(href); //释放掉blob对象
    },
原文地址:https://www.cnblogs.com/miaSlady/p/13151280.html