Uploaded Files and Upload Handlers¶
Uploaded files¶
During file uploads, the actual file data is stored in request.FILES. Each entry in this dictionary is an
UploadedFile object (or a subclass) – a wrapper around an uploaded file.
You’ll usually use one of these methods to access the uploaded content:
-
UploadedFile.read()¶ Read the entire uploaded data from the file. Be careful with this method: if the uploaded file is huge it can overwhelm your system if you try to read it into memory. You’ll probably want to use
chunks()instead; see below.
-
UploadedFile.multiple_chunks(chunk_size=None)¶ Returns
Trueif the uploaded file is big enough to require reading in multiple chunks. By default this will be any file larger than 2.5 megabytes, but that’s configurable; see below.
-
UploadedFile.chunks(chunk_size=None)¶ A generator returning chunks of the file. If
multiple_chunks()isTrue, you should use this method in a loop instead ofread().In practice, it’s often easiest to use
chunks()all the time. Looping overchunks()instead of usingread()ensures that large files don’t overwhelm your system’s memory.
Here are some useful attributes of UploadedFile:
-
UploadedFile.name¶ The name of the uploaded file (e.g.
my_file.txt).
-
UploadedFile.size¶ The size, in bytes, of the uploaded file.
-
UploadedFile.content_type¶ The content-type header uploaded with the file (e.g. text/plain or application/pdf). Like any data supplied by the user, you shouldn’t trust that the uploaded file is actually this type. You’ll still need to validate that the file contains the content that the content-type header claims – “trust but verify.”
-
UploadedFile.content_type_extra¶ A dictionary containing extra parameters passed to the
content-typeheader. This is typically provided by services, such as Google App Engine, that intercept and handle file uploads on your behalf. As a result your handler may not receive the uploaded file content, but instead a URL or other pointer to the file (see RFC 2388).
-
UploadedFile.charset¶ For text/* content-types, the character set (i.e.
utf8) supplied by the browser. Again, “trust but verify” is the best policy here.
Note
Like regular Python files, you can read the file line-by-line by iterating over the uploaded file:
for line in uploadedfile:
do_something_with(line)
Lines are split using universal newlines. The following are
recognized as ending a line: the Unix end-of-line convention '\n', the
Windows convention '\r\n', and the old Macintosh convention '\r'.
Subclasses of UploadedFile include:
-
class
TemporaryUploadedFile[source]¶ A file uploaded to a temporary location (i.e. stream-to-disk). This class is used by the
TemporaryFileUploadHandler. In addition to the methods fromUploadedFile, it has one additional method:
-
TemporaryUploadedFile.temporary_file_path()[source]¶ Returns the full path to the temporary uploaded file.
-
class
InMemoryUploadedFile[source]¶ A file uploaded into memory (i.e. stream-to-memory). This class is used by the
MemoryFileUploadHandler.
Built-in upload handlers¶
Together the MemoryFileUploadHandler and
TemporaryFileUploadHandler provide Django’s default file upload
behavior of reading small files into memory and large ones onto disk. They
are located in django.core.files.uploadhandler.
File upload handler to stream uploads into memory (used for small files).
Upload handler that streams data into a temporary file using
TemporaryUploadedFile.