Erik
Erik

Reputation: 14750

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.

Upvotes: 15

Views: 9833

Answers (2)

Kelvin Low
Kelvin Low

Reputation: 400

res.download(filePath, req.param('file'), function(err){
  //CHECK FOR ERROR
  fs.unlink(filePath);
});

Upvotes: 5

Jonathan Wiepert
Jonathan Wiepert

Reputation: 1242

Use the callback in the download api:

res.download(filePath, req.param('file'), function(err){
  //CHECK FOR ERROR
  fs.unlink(filePath);
});

Upvotes: 21

Related Questions