问题
I am using the TemporaryFileUploadHandler to upload files. If a user is uploading a large file and cancels the upload, the file remains in my temporary directory.
Is there a way to trap a cancelled upload (connection reset before a file was fully uploaded) in order to cleanup these files?
The only alternative I can think of is a cron job which looks at the temp directory and deletes files which have not been updates in some reasonable amount of time.
回答1:
Not sure if it helps, but you may try to connect to django request signals:
request_finished - Sent when Django finishes processing an HTTP request.
got_request_exception - This signal is sent whenever Django encounters an exception while processing an incoming HTTP request.
I think Django should raise error if connection is aborted, so the usage of second one is probably a solution. Please let me know it it helps.
来源:https://stackoverflow.com/questions/8670982/cleanup-partly-uploaded-files-handled-by-temporaryfileuploadhandler-in-django