我有一个exceljs
用来生成 excel 文件的sails 应用程序。查看他们的文档:
https://www.npmjs.com/package/exceljs#writing-xlsx
看来他们允许写入流。
// write to a stream
workbook.xlsx.write(stream)
.then(function() {
// done
});
当用户期待响应时,我该怎么做?
我阅读了以下内容:
http://sailsjs.org/documentation/concepts/custom-responses/adding-a-custom-response
http://sailsjs.org/documentation/concepts/custom-responses/default-responses
res.attachment()
保存文件后我也尝试过,但没有成功。有什么我想念的吗?
编辑
下面的答案几乎是正确的。我做了以下,它的工作。以前的解决方案有时会损坏文件。
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
res.setHeader("Content-Disposition", "attachment; filename=" + "Report.xlsx");
return workbook.xlsx.write(res)
.then(function() {
res.end();
});
这适用于我的 EC2 实例和本地。