I have an express get route that downloads a CSV file in the browser. That part works fine, but now I want to delete the file using fs.unlink after it is downloaded because it is being stored on the host os in the temporary directory and they build up.
If I try to run the code snipped below, it cannot complete the download because it gets deleted by unlink before it can process (I think).
router.get('/downloadCsv', function(req, res, next) {
res.download(downloadCsvPath);//download file
fs.unlink(downloadCsvPath, (err) => {//delete file from server os
if (err) {
console.error(err);
} else {
console.log('removed: ' downloadCsvPath);
}
});
});
It gives this error:
Error: ENOENT: no such file or directory, stat '/var/folders/fw/03jxpm311vs548bvf9g_1vxm0000gq/T/<filename>.csv'
As a workaround, I added a 3 second timeout to let the file download first, then delete:
router.get('/downloadCsv', function(req, res, next) {
res.download(downloadCsvPath);//download file
setTimeout(() => {
fs.unlink(downloadCsvPath, (err) => {
if (err) {
console.error(err);
} else {
console.log(downloadCsvPath);
}
});
}, 3000);
});
This works, I get the file downloaded and then it gets deleted from the temporary directory.
But, if connection was poor and it takes longer than 3 seconds, it would probably fail again. There has got to be a better way to run this fs.unlink block of code only once the res.download finishes. I am new to express js so I don't know how to do this. I want to do something like:
res.download(path).then(() => {
//do something
});
But it does not offer this.
I want to run a function after my expressjs res.download, but I do not know a way other than setTimeout
CodePudding user response:
As per Express documentation, res.download()
can accept a callback function, which is invoked when the transfer is complete or when an error occurs:
router.get('/downloadCsv', (req, res, next) => {
res.download(downloadCsvPath, (err) => {
if (err) {
// Handle error, but keep in mind the response may be partially-sent
// so check res.headersSent
} else {
fs.unlink(downloadCsvPath, (err) => {
if (err) {
console.error(err);
} else {
console.log(downloadCsvPath);
}
});
}
});
});
Here is the link to the relevant section in the Express documentation.
Hope this helps.