Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know when file downloading is finished?

I need to get an user download his file and remove it after response get finished:

app.get('/download/:file', function (req, res) {
   var filePath = '/files/' + req.param('file');
   res.download(file);

   fs.unlink(filePath); 
});

In the code above fs.unlink could invoked early than res.download will get finished.

like image 408
Erik Avatar asked May 02 '13 05:05

Erik


1 Answers

Use the callback in the download api:

res.download(filePath, req.param('file'), function(err){
  //CHECK FOR ERROR
  fs.unlink(filePath);
});
like image 158
Jonathan Wiepert Avatar answered Sep 28 '22 09:09

Jonathan Wiepert