【发布时间】:2022-01-26 06:22:47
【问题描述】:
我有一个 nodeJs 服务器和 react 应用程序。在我的NodeJs中,我返回了一个excel文件,单击发送和下载时,我可以在邮递员中下载它。但是在反应文件是下载但打开时出错,它已损坏
这是我在 ReactJS 中的实现(这就是问题所在)
export const exportUsersToExcel = async (token) => {
try {
axios
.get(
`${process.env.REACT_APP_SERVER}/api/dashboard/users/excel`,
{
headers: { "auth-token": token },
},
{ responseType: "blob" }
)
.then((response) => {
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement("a");
link.href = url;
link.setAttribute("download", "Users.xlsx");
document.body.appendChild(link);
link.click();
});
} catch (error) {
console.log("Error Exporting Users");
return error;
}
};
我正在像这样在 NodeJS 中发送文件
res.set({
"Content-disposition": `attachment; filename=Users.xlsx`,
"Content-Type":
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
});
return workbook.xlsx.write(res).then(() => {
res.status(200).end();
});
【问题讨论】: