<a href="文件链接" download='下载文件名'></a> <--!但是其中的download对应音频文件和视频文件无效-->
<script> const a = document.createelement('a'); a.setattribute('href', '文件链接'); //a.href='文件链接' a.setattribute('download', '文件名'); //a.download='文件名' a.click(); </script>
写代码的思路
先请求音频的链接,再把返回值转换成二进制,再根据他二进制对象生成新链接,再创建a标签,点击a标签
//这是vue里面的写的普通页面也差不多 <script> this.$axios({ method: 'get', url: row.src, responsetype: '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();
如对本文有疑问, 点击进行留言回复!!
2016年信息安全工程师综合知识第11-15题解析【建群网培信息安全工程师】
网友评论