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>