首页 > 其他分享 >a标签下载文件 带token

a标签下载文件 带token

时间:2024-01-23 16:16:15浏览次数:21  
标签:const 标签 headers token blob error document 下载

const downloadfile = (url, token) => {
const headers = new Headers();
headers.append('Authorization', token); // 设置token
// 发起 Fetch 请求
fetch(url, {
method: 'GET',
headers: headers,
}).then(res => res.blob()).then(blob => {
const a = document.createElement('a');
a.href = window.URL.createObjectURL(blob);
a.download = '违法代码报警.xls'; // 设置下载文件名
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}) .catch(error => console.error('下载失败:', error));
};

标签:const,标签,headers,token,blob,error,document,下载
From: https://www.cnblogs.com/lihong-123/p/17982669

相关文章