I successfully create a excel file with excel4node and save it in the server, then I use a read stream to send the file to the client:
res.set({
'Content-Type': 'application/vnd.openxmlformats',
'Content-Disposition': 'attachment; filename='+filename
});
// This line opens the file as a readable stream
var readStream = fs.createReadStream(path+filename, {encoding: 'utf8'});
// This will wait until we know the readable stream is actually valid before piping
readStream.on('open', function () {
// This just pipes the read stream to the response object (which goes to the client)
readStream.pipe(res);
});
// This catches any errors that happen while creating the readable stream (usually invalid names)
readStream.on('error', function(err) {
res.end(err);
});
After that I get the data in the browser and download it using Blob:
var blob = new Blob([data], { type: 'application/vnd.openxmlformats' });
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = "File.xlsx";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
When I try to open the file I get the following message:
"Excel found unreadable content in 'File.xlsx'. Do you want to recover the contents of this workbook? If you trust the source of this workbook, click Yes"
And if I click yes, excel says that the file is corrupt and can't be recovered.
I would like to know what can I do to retrieve the excel file from the server.