I am trying create and send an excel file to client. Client should download the file via ajax request, because I need filter parameters.
I am using excel4node package to create an excel file.
I write the code below and it's working for now but i am suspicious what if I will have data bigger than buffer. Is this the right way of using buffer? (please check the line with writeToBuffer method)
const xl = require('excel4node');
const excelCreator = function (data) {...}
app.post('/api/excel', jsonParser, (req, res) => {
let reqObj = {
method: 'post',
url: apiUrl + '/MemberService';,
headers: {
'Content-Type': 'application/json'
},
data: req.body
};
axios(reqObj)
.then(response => {
res.body = responseHandler(response); // a helper function to set res object
let data = res.body.Data;
res.setHeader('Content-Disposition', 'attachment; filename=' + 'excel.xlsx');
res.type('application/octet-stream');
res.body.Data = null;
excelCreator(data).writeToBuffer().then(function (buffer) {
res.body.Data = buffer;
res.send(res.body);
});
})
.catch(...);
});