Add a PersistedTemporaryFileUploadHandler for file upload

Skip to first unread message


Dec 16, 2023, 8:02:00 PM12/16/23
to Django developers (Contributions to Django itself)
Using the PersistedTemporaryFileUploadHandler upload handler, Django will write the uploaded file to a temporary file stored in your system's temporary directory, as the TemporaryFileUploadHandler already do. However, the file this time will not be suppressed after your first read, and will persist until you delete it.

PersistedTemporaryFileUploadHandler class inherit from the TemporaryFileUploadHandler class. No big changes, only to set the delete args of the NamedTemporaryFile to False

Use cases:

  • One use case that I personally faced is having many threads reading from the same uploaded file. When one of the threads reads the file content, the file is automatically deleted, and so became unavailable for the other threads.
  • Another use case was find in a stack overflow question, where a user was looking for a way to control the life time of the uploaded file. 
Even thought it may be easy to forbid the temporary file from being deleted, I think it's a necessary feature or class to have withing the framework as it's a behavior that I would naturally consider available in such a great framework.

Jörg Breitbart

Dec 18, 2023, 3:35:41 AM12/18/23
My first reflex to this is -1 for a simple reason:

the persistence layer in django is meant to be db-driven. The upload
logic is transient for that reason, and nothing stops you from creating
a better controlled file upload handling with a model holding those
files before entering your real business logic.

Adding another low-level persistent layer will create a lot of fuzz,
esp. around the right way to evict those entries later on. And ppl will
forget to delete those entries, if they are in doubt. Not a good idea.
Reply all
Reply to author
0 new messages