如何知道文件下载何时完成?

Eri*_*rik 9 download node.js express

我需要让用户下载他的文件并在响应完成后将其删除:

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

   fs.unlink(filePath); 
});
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,fs.unlink可以比res.download更早地调用.

小智 9

在下载api中使用回调:

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


小智 5

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