I am using exceljs in my route to create an excel file and trying to download the same file on my browser. But the corrupted file is getting downloaded.
Also, the file is getting downloaded as expected when I tried hitting the endpoint using POSTMAN.
I did try to look at the https://github.com/exceljs/exceljs/issues/37 , but it didn't helped much.
//Backend Code:
//My route file code, wherein workbook = new Excel.Workbook();
var fileName = 'FileName.xlsx';
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
res.setHeader('Content-Disposition', 'attachment; filename=' + fileName);
return workbook.xlsx.write(res).then(function(){
res.end();
});
//Client Side Code:
$.ajax({
url: "/*****/download",
type: "GET",
data: body,
headers: {
'Content-type': 'application/json'
},
responseType: 'blob',
success: function (data) {
//Data contains the fields that I want to download
var saving = document.createElement('a');
var csvData;
//Using a FileSaver or Download library to download the file.
saveAs(new Blob([data], { type: "vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8" }), 'test.xlsx');
document.body.appendChild(saving);
saving.click();
document.body.removeChild(saving);
},
error: function (error) {
console.log(error);
}
});
While opening the excel file, it says, We found a problem with some content....Do you want to recover ...........