I have a route on a node server that generates a excel file and is downloaded.
This is the part of the route that creates the xlsx file and responds.
workbook.toFileAsync("./public/" + empresa2 + ".xlsx").then(() => {
res.download( __dirname + "/public/" + empresa2 + ".xlsx")
})
.catch(error => {
res.send({error: 'Error'})
})
I call this route with and ajax GET call and in the success i retrieve the file with the window.location method.
This works fin for me. Now i want to delete the file after it downloads. So far i understand that this has to be done on server side.
i have a route that unlinks the file
fs.unlink( __dirname + "/public/" + empresa2 + ".xlsx",function(error){
console.log(error);
});
It works well, my problem is when and where to call that route so that it does not delete the file before it is downloaded.
if called in the success of the ajax get, the file is deleted faster than the download. so far i have tried a confirm dialogue and timeout.
Thanks for helping.