axios.get(url, {
responseType: 'blob',
}).then(response => {
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
Link.style.display = "none";
let fname = '考勤导入模版.xlsx';
link.href = url;
link.setAttribute('download', fname);
document.body.appendChild(link);
link.click();
document.body.removeChild(Link); //下载完成移除元素
window.URL.revokeObjectURL(url);
}).catch(error => {
console.log('error:' + JSON.stringify(error))
});
axiost(
method: 'get',
url: url, // 请求地址
data: options, // 参数
responseType: 'blob' // 表明返回服务器返回的数据类型
).then(response => {
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
Link.style.display = "none";
let fname = '考勤导入模版.xlsx';
link.href = url;
link.setAttribute('download', fname);
document.body.appendChild(link);
link.click();
document.body.removeChild(Link); //下载完成移除元素
window.URL.revokeObjectURL(url);
}).catch(error => {
console.log('error:' + JSON.stringify(error))
});
这段代码展示了如何使用axios库在浏览器环境中实现文件的下载。通过设置responseType为'blob',然后创建一个Blob对象,并利用URL.createObjectURL方法生成一个可下载的链接。最后,创建一个隐藏的a标签,设置其href属性为生成的链接,并触发点击事件来开始下载。文件名可自定义,如'考勤导入模版.xlsx'。

2267

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



