//--------------xxx/api.ts------------
export const exportList = async (body:API.export) => {
return fetch('/api/export', { //导出接口
method: 'POST',
headers: {
'Content-Type': 'application/json',
token: sessionStorage.getItem('token') || ''
},
body: JSON.stringify(body),
credentials: "include"
})
}
// -------------yourfile.tsx-----------
let response = await exportList(info) //传参数,调用接口
let blob = await response.blob();
let reader = new FileReader();
reader.readAsDataURL(blob); //读取数据
reader.onload = function (e: any) {
let a = document.createElement('a');
let date = new Date().getTime() //时间戳作为导出文件名
a.download = `${date}.xlsx`;
a.href = e.target.result;
document.body.insertAdjacentElement('afterend', a);
a.click();
a.remove();
}