<a href="文件连接" download='下载文件名'></a> <--!可是其中的download对应音频文件和视频文件无效-->
<script> const a = document.createElement('a'); a.setAttribute('href', '文件连接'); //a.href='文件连接' a.setAttribute('download', '文件名'); //a.download='文件名' a.click(); </script>
写代码的思路html
先请求音频的连接,再把返回值转换成二进制,再根据他二进制对象生成新连接,再建立a标签,点击a标签
vue
//这是vue里面的写的普通页面也差很少 <script> this.$axios({ method: 'get', url: row.src, responseType: 'blob' //这个不能少,让response二进制形式,若是你按照网上教程不设置这个将返回值进行BLOB([])进行处理可能会出现解析错误 }).then(response => { const href = URL.createObjectURL(response.data); //根据二进制对象创造新的连接 const a = document.createElement('a'); a.setAttribute('href', href); a.setAttribute('download', row.title); a.click(); URL.revokeObjectURL(href); } </script>
//原理和ajax如出一辙 function request() { fetch('<接口地址>', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: '<请求参数:json字符串>', }) .then(res => res.blob()) .then(data => { let blobUrl = window.URL.createObjectURL(data); download(blobUrl); }); } function download(blobUrl) { const a = document.createElement('a'); a.download = '<文件名>'; a.href = blobUrl; a.click(); } request();