KVISH KVISH - 2 months ago 50
Node.js Question

Sailsjs download a stream of data

I have a sails application that I'm using

exceljs
to generate an excel file. Looking at their documenatation:

https://www.npmjs.com/package/exceljs#writing-xlsx

It appears they allow writing to a stream.

// write to a stream
workbook.xlsx.write(stream)
.then(function() {
// done
});


How can I do this when the user is expecting a response?

I read the below:

http://sailsjs.org/documentation/concepts/custom-responses/adding-a-custom-response

http://sailsjs.org/documentation/concepts/custom-responses/default-responses

I also tried
res.attachment()
after saving the file and was not successful. Is there something i'm missing?

Answer

Solution: https://github.com/guyonroche/exceljs/issues/37 write the workbook to the response.

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();
Comments