zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    看淘宝营销api 文档有感
    创业公司如何做好数据驱动的开发工作
    docker 常见错误总结
    从npm 角度理解 mvn 的 pom.xml
    best practices for designing web api
    我对自动化测试的一些认识
    Docker学习笔记
    Docker命令学习
    JVM学习笔记三:垃圾收集器与内存分配策略
    JVM学习笔记二:JVM参数
  • 原文地址:https://www.cnblogs.com/yesu/p/10008303.html
Copyright © 2011-2022 走看看