简体   繁体   English

如何处理Node.js中的文件上传中断

[英]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. 当访问者在将文件上传到我的Node.js服务器时按下STOP,REFRESH或BACK或只是关闭浏览器时,就会出现问题。 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. 但是,显然Node.js锁定了上次上载的文件,从而阻止了该文件的删除。

The lock on the file will be released when I stop the Node.js server, when it will be deleted finally. 当我停止Node.js服务器时,文件上的锁将被释放,最后将其删除。

How can I delete the garbage files without restarting the server? 如何在不重新启动服务器的情况下删除垃圾文件? I am using the busboy package. 我正在使用busboy软件包。

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

Well. 好。 I solved the problem for busboy by ending the stream manually after 30 minutes. 我通过30分钟后手动结束流来解决了busboy的问题。 This unlocks the file for deletion next time round. 这将解锁文件,以在下一次删除。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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