简体   繁体   中英

How to deal with Interrrupted File Uploads in Node.js

The problem arises when the visitor presses STOP, REFRESH, or BACK or simply closes the browser during file uploads to my Node.js server. There will be some garbage files left in the server.

To deal with this problem, I tried to save the uploaded files to a temporary directory first and delete the files the next time the visitor uploads something again. However, apparently Node.js locks the file being uploaded last time, thus preventing it from being deleted.

The lock on the file will be released when I stop the Node.js server, when it will be deleted finally.

How can I delete the garbage files without restarting the server? I am using the busboy package.

我会使用Skipper之类的包来上传文件,您只在上传完成后才将文件保存到磁盘,如果用户中断了中间位置的上传,则文件将永远不会保存,您只需清除缓冲区即可。

Well. I solved the problem for busboy by ending the stream manually after 30 minutes. This unlocks the file for deletion next time round.

setTimeout(function(){fstream.end();},1800000);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM