关于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