Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我需要让用户下载他的文件并在响应完成后将其删除:
app.get('/download/:file', function (req, res) { var filePath = '/files/' + req.param('file'); res.download(file); fs.unlink(filePath); });
在上面的代码中, fs.unlink 可能会在 res.download 完成之前被调用。
在下载 api 中使用回调:
res.download(filePath, req.param('file'), function(err){ //CHECK FOR ERROR fs.unlink(filePath); });