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>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/159674.html