關於VUE實現文件下載的多種方法


:問題主要出現在項目中引用mockjs 影響原生ajax的responseType,導致配置blob方式無法生效,注釋引用mockjs,實現文檔流下載

  1 // 方法一  直接打開新標簽頁,IE會出現兼容問題
  2 window.open('/api/delivery/channel/export?' + paramsString)
  3 // 方法二  調用插件 npm install --save ly-downloader
  4 download(1,'/api/delivery/channel/export?' + paramsString, 'kkk' )
  5 // 方法三  action方法,傳參后創建blob對象
  6 this.asyExportchannelexcel().then((res) => {
  7   // console.log(res)
  8   // let data = "\ufeff" + res.data
  9   let blob = new Blob([res.data], {
 10       type: 'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
 11   })
 12   if (window.navigator.msSaveOrOpenBlob) {
 13       navigator.msSaveBlob(blob);
 14   } else {
 15       let url = window.URL.createObjectURL(blob)
 16       let elink = document.createElement('a');
 17       elink.download = "報表.xls";
 18       elink.style.display = 'none';
 19       elink.href = url
 20       document.body.appendChild(elink);
 21       elink.click();
 22       document.body.removeChild(elink);
 23   }
 24 })
 25 // 方法四  創建xhr對象,實現請求
 26 let xhr = new XMLHttpRequest()
 27 let fileName = 'file.xls' // 文件名稱 
 28 xhr.open('GET', '/api/delivery/channel/export?' + paramsString, true)
 29 xhr.responseType = 'arraybuffer'
 30 console.log(xhr)
 31 // xhr.setRequestHeader(token, 'xxxxx') // 請求頭中的驗證信息等(如果有)
 32 xhr.onload = function() {
 33   if (this.status === 201) {
 34     let type = xhr.getResponseHeader('Content-Type')
 35     console.log(type)
 36     let blob = new Blob([this.response], {type: type})
 37     if (typeof window.navigator.msSaveBlob !== 'undefined') {
 38       /*
 39        * IE workaround for "HTML7007: One or more blob URLs were revoked by closing
 40        * the blob for which they were created. These URLs will no longer resolve as 
 41        * the data backing the URL has been freed." 
 42        */
 43       window.navigator.msSaveBlob(blob, fileName)
 44     } else {
 45       let URL = window.URL || window.webkitURL
 46       let objectUrl = URL.createObjectURL(blob)
 47       if (fileName) {
 48         var a = document.createElement('a')
 49         // safari doesn't support this yet
 50         if (typeof a.download === 'undefined') {
 51           window.location = objectUrl
 52         } else {
 53           a.href = objectUrl
 54           a.download = fileName
 55           document.body.appendChild(a)
 56           a.click()
 57           a.remove()
 58         }
 59       } else {
 60         window.location = objectUrl
 61       }
 62     }
 63   }
 64 }
 65 xhr.send()
 66 // 方法五  原生axios實現請求
 67 let info = {
 68   department6Name: this.$store.state.project.channeldepartment6,
 69   department7Name: this.$store.state.project.channeldepartment7,
 70   quotalevel: this.$store.state.project.channelquotalevel,
 71   step: this.$store.state.project.channelstep,
 72   deliveryMethod: this.$store.state.project.channeldeliveryMethod,
 73   contract_sign_time: this.$store.state.project.channelyear
 74 }
 75 axios({
 76     method: 'GET',
 77     url: '/api/delivery/channel/export',
 78     header: {
 79         contentType: "application/x-www-form-urlencoded; charset=utf-8"
 80     },
 81     params: info,
 82     responseType: 'blob'
 83   }).then(res=>{
 84     console.log(res)
 85     let blob = new Blob([res.data], {type: "application/vnd.ms-excel;charset=utf-8"});
 86     console.log(blob)
 87     let fileName = '渠道交付.xls'
 88     if (window.navigator.msSaveOrOpenBlob) { // IE10
 89       navigator.msSaveBlob(blob, fileName);
 90     } else {
 91       let link = document.createElement('a');
 92       link.style.display = 'none';
 93       link.href = URL.createObjectURL(blob); //創建一個指向該參數對象的URL
 94       link.download = fileName;
 95       link.click(); // 觸發下載
 96       URL.revokeObjectURL(link.href); // 釋放通過 URL.createObjectURL() 創建的 URL
 97     }
 98   }).catch(err=>{
 99     console.log(err)
100   })
101 }

 


免責聲明!

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



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