下载后Express.js删除文件(本地FS)
我有以下Express Handler的代码,该代码旨在在目录条目中找到有效的文件路径后下载文件 - 处理程序具有命令行选项,在成功下载后可以删除该文件:
res.download(filePath, err => {
if (err) {
return next(err)
}
if (AUTO_DELETE_ENABLED) {
rmSync(filePath)
console.log('deleted after download...')
}
})
在浏览器中,输入有效的文件路径时,下载似乎是 begin ,但是该文件立即删除,而无需等待。
有人可以向我解释我该怎么做吗? express res.download
接听回调,我正在使用该回调来动态确定命令行设置是否已启用,如果是这样 - 我删除了文件。
那为什么这不起作用呢?我该怎么做才能确保发生这种情况?
目前,我有一个3000ms settimeout
可以确保达到所需的功能,但是有些视频可能需要花费更长的下载...
q:我正在使用的事实。 rmsync
可能是问题的一部分?该代码的其余部分在大多数情况下是异步的。
任何帮助!
I've got the following code for an Express handler which aims to download a file after a valid file path is found in the directory entries - the handler has a command-line option which is in place to delete said file, after a successful download:
res.download(filePath, err => {
if (err) {
return next(err)
}
if (AUTO_DELETE_ENABLED) {
rmSync(filePath)
console.log('deleted after download...')
}
})
In a browser, when entering the valid file path, the download seems to begin, but the file is deleted right away, without waiting.
Can someone explain to me how I can do this? Express res.download
takes a callback, I'm using that callback to dynamically determine if the command line setting is enabled, if so - I delete the file.
So why does this not work? And what can I do to ensure this happens?
For now, I have a 3000ms setTimeout
which can make sure the desired functionality is achieved, but some videos may take longer than that to download...
Q: Is the fact I'm using rmSync
potentially part of the problem? The rest of the code is for the most part asynchronous.
Any help appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用UNINK功能:
https://nodejs.org/api/api/fs.html#fs.html#fs_file_sysystem
您可以使用删除完成后或发生某些错误时,回调函数要发出信号。
You shoul use unlink function:
https://nodejs.org/api/fs.html#fs_file_system
You can use callback function to signal when deleting is finished or if some error occured.