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!
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.