vue下載excel文件,后台傳過來的是文件流解決辦法


 

 

 

 

 downErrorExcel() {
      getdownAjax(
        url +
          upmsUrl +
          "/admin/teacher/download/excel/error/" +
          this.uploadError_downId,
        { responseType: "blob" }
      ).then(res => {
        let blob = new Blob([res], { type: "application/vnd.ms-excel" }); // res就是接口返回的文件流了
        let objectUrl = URL.createObjectURL(blob);
        window.location.href = objectUrl;
      });
    },
 
 

 

 

 ========================================方二
//導出excel表
Vue.prototype.exportExcel = (Url, FileName) => {
  getdownAjax(Url, {
    responseType: "blob"
  }).then(res => {
    // console.log("res", res);
    if (res) {
      let blob = new Blob([res.data], {
        type: "application/vnd.ms-excel;charset=utf-8"
      }); // res就是接口返回的文件流了
      let objectUrl = URL.createObjectURL(blob);
      // console.log(objectUrl);
      // const fileName = FileName;
      const elink = document.createElement("a");
      elink.download = FileName; //下載文件名稱,
      elink.style.display = "none";
      elink.href = objectUrl;
      document.body.appendChild(elink);
      elink.click();
      URL.revokeObjectURL(elink.href); // 釋放URL 對象
      document.body.removeChild(elink);
    }
  });
}

 

 tablebtn_exportAll() {
      this.exportExcel(
        url + upmsUrl + "/admin/student/export",
        "學員列表.xlsx"
      );
    },

 

 


免責聲明!

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



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