axios({
method:'get',
url:'url地址',
responseType:'blob',
}).then(res=>{
const link=document.createElement('a');
let blob=new Blob([res.data],{type:'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'})
link.style.display='none',
link.href=URL.createObjectURL(blob);
link.downLoad='下载文件名',
document.body.appendChild(link);
link.click();
document.body.removeChild(link)
}).catch(error=>{
throw error;
})
使用axios下载Excel文件
这段代码演示了如何使用axios库通过GET请求获取数据,并将响应转换为blob类型以创建一个Excel文件供用户下载。它涉及到了文件下载、Blob对象、URL.createObjectURL方法以及模拟点击链接触发下载。
1万+

被折叠的 条评论
为什么被折叠?



