0

我正在使用带有 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-populatehttps ://github.com/dtjohnson/xlsx-populate#serving-from-express

4

1 回答 1

0

尝试使用 Postman 中的“发送和下载”按钮,只需单击“发送”按钮上的向上箭头,您应该会看到此选项。

发送和下载

然后,当您发送请求时,系统会提示您下载“output.xlsx”文件。

我认为您不需要进行任何编码更改。

于 2019-07-18T09:55:37.213 回答