Search code examples
javascriptnode.jsfile-uploaduploadbusboy

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.


Solution

  • I would use a package like Skipper to upload files, you only save the file to disk when the upload is completed, if the user breaks off the upload somewhere in the middle then the file is never saved and you just clear the buffer.