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