I'm Trying to download to the client an xls file created in the nodejs (using exceljs). The creation is based on http://www.ihamvic.com/2018/07/25/create-and-download-excel-file-in-node-js/
For some reason - I can't save the file in the client - I'm getting "Http failure during parsing for " when subscribing to the getExcel observable. Am I missing some header definition ? See my code:
This is the nodejs side:
const express = require('express');
const router = express.Router();
const excel = require('./excel');
router.use(req, res, next) =>
{
//The getDataByPromise is a function that return a promise
return getDataByPromise(req.body.request).then((dataResult) => {
let reportName = req.body.reportName ? req.body.reportName : '';
return excel.createExcel(res, dataResult, reportName);
}).catch((err) => {
next({
details: err
})
});
})
module.exports = router;
This is the excel module with the createExcel function:
module.exports =
{
createExcel : function(res, dataResult, reportTypeName)
{
let workbook = new excel.Workbook();
let worksheet = workbook.addWorksheet('sheet1');
dataResult.forEach(dataItem => worksheet.addRow(dataItem)); //Insert data into the excel
var tempfile = require('tempfile');
var tempFilePath = tempfile('.xlsx');
console.log("tempFilePath : ", tempFilePath);
workbook.xlsx.writeFile(tempFilePath).then(function()
{
res.sendFile(tempFilePath, function(err)
{
if (err)
{
console.log('---------- error downloading file: ', err);
}
});
console.log('file is written');
});
}
}
This is the service approaching the nodejs (we'll call it srv) in the client:
getExcel(request : any , reportName : string ) : Observable<any>
{
var path = <relevant path to the nodejs endpoint>;
const options = { withCredentials: true };
return this.http.post<any>(path, {request: request, reportName : reportName }, options) //This route to the getDataByPromise function
}
This is the component function:
exportToExcel() : void
{
this.srv.getExcel(votingBoxRequestForExcel, this.reportTypeNameForExcel).subscribe(result =>
{
const data: Blob = new Blob([result], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8'});
//FileSaver is file-saver package
FileSaver.saveAs(data, 'test.xlsx');
},
error => console.log(error) //Reaching to the error instead of the response
);
}