vue導出文件下載


項目當中有用到文件的導出功能,以此來總結

request({
        /*url: this.exportUrl,*/
        url: `************`,
        method: "GET",
        responseType: "blob"
      }).then(res => {
        console.log(res); 
        var blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8' }); //application/vnd.openxmlformats-officedocument.spreadsheetml.sheet這里表示xlsx類型
         
        if (window.navigator.msSaveBlob) {  //沒有此判斷的話,ie11下的導出沒有效果
          window.navigator.msSaveBlob(blob, unescape(res.headers.filename.replace(/\\u/g, '%u')));
        } else {
          var downloadElement = document.createElement('a');  
          var href = window.URL.createObjectURL(blob); //創建下載的鏈接
            
          downloadElement.href = href;  
          downloadElement.download = unescape(res.headers.filename.replace(/\\u/g, '%u')); //下載后文件名
            
          document.body.appendChild(downloadElement);  
          downloadElement.click(); //點擊下載
            
          document.body.removeChild(downloadElement); //下載完成移除元素
            
          window.URL.revokeObjectURL(href); //釋放掉blob對象 
        } 

      })

 注:有的是自己已經做了ie11下的promise的處理,有的人可能沒有做ie11下的promise通用,promise在ie下報錯的小伙伴請npm安裝下

 npm install  @babel/polyfill

 


免責聲明!

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



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