vue下载本地文件 下载二进制流文件 兼容ie


vue-cli2要下载的静态文件放在static目录下,vue-cli3则放在public目录下

ie不支持 h5 的download写法,故用以下写法

<el-button type="text" @click="download">aa.excel</el-button>
download(){
      axios.get('static/aa.excel', {
        responseType: 'blob', 
        }).then(response => {
          if ("msSaveOrOpenBlob" in navigator) {//ie
              var data = response.data;
              var blob = new Blob([data], { type: "application/vnd.ms-excel" });
              window.navigator.msSaveOrOpenBlob(blob, 'aa.excel');
              return;
          } else {
            const url = window.URL.createObjectURL(new Blob([response.data]));
            const link = document.createElement('a');
            let fname = 'aa.excel';
            link.href = url;
            link.setAttribute('download', fname);
            document.body.appendChild(link);
            link.click();
          }
        });
    },

若不需要兼容ie浏览器,则可直接用h5 download

<a href="static/aa.excel" download="aa.excel">aa.excel</a>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM