Sailsjs 下载数据流
Sailsjs download a stream of data
我有一个 sails 应用程序,我正在使用 exceljs
生成 excel 文件。查看他们的文档:
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 实例和本地。
解决方法:https://github.com/guyonroche/exceljs/issues/37将工作簿写入响应。
var Excel = require('exceljs');
var workbook = new Excel.Workbook();
var sheet = workbook.addWorksheet("My Sheet");
res.setHeader('Content-Type', 'application/vnd.openxmlformats');
res.setHeader("Content-Disposition", "attachment; filename=" + "Report.xlsx");
workbook.xlsx.write(res);
res.end();
我有一个 sails 应用程序,我正在使用 exceljs
生成 excel 文件。查看他们的文档:
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 实例和本地。
解决方法:https://github.com/guyonroche/exceljs/issues/37将工作簿写入响应。
var Excel = require('exceljs');
var workbook = new Excel.Workbook();
var sheet = workbook.addWorksheet("My Sheet");
res.setHeader('Content-Type', 'application/vnd.openxmlformats');
res.setHeader("Content-Disposition", "attachment; filename=" + "Report.xlsx");
workbook.xlsx.write(res);
res.end();