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對象
    },


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM