我正在使用带有 node.js 的 xlsx-populate 模块。正如他们网站上所述,我将工作簿作为缓冲区发送。我正在使用邮递员访问 API 并获取 xlsx 文件作为附件。但是我得到的响应有奇怪的字符,如下所示:
�2�N��N��+xl/drawings/drawing1.xml����0��>ٻ=C���@`ChB�M�
��M���8�̗�����ً��5��2��i
const workbook = await XlsxPopulate.fromFileAsync(filename);
// await workbook.toFileAsync(outfile);
const data = await workbook.outputAsync();
res.attachment("output.xlsx");
res.send(data);
当我用来await workbook.toFileAsync(outfile);在本地保存文件时,一切正常,我可以读取文件。但是当我尝试使用命令res.attachment("output.xlsx");和发送文件以下载时res.send(data);,我在对 Postman 的响应中得到了奇怪的字符。
响应标头有 11 个属性,其中包括:
content-disposition -> attachment; filename="output.xlsx"
content-type -> application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
使用 express 下载 xlsx 文件的类似步骤也在 GitHub 页面上提到xlsx-populate:
https ://github.com/dtjohnson/xlsx-populate#serving-from-express
