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