1
.. _topics-file-uploads:
7
.. currentmodule:: django.core.files
11
Most Web sites wouldn't be complete without a way to upload files. When Django
12
handles a file upload, the file data ends up placed in ``request.FILES`` (for
13
more on the ``request`` object see the documentation for :ref:`request and
14
response objects <ref-request-response>`). This document explains how files are
15
stored on disk and in memory, and how to customize the default behavior.
20
Consider a simple form containing a ``FileField``::
22
from django import forms
24
class UploadFileForm(forms.Form):
25
title = forms.CharField(max_length=50)
26
file = forms.FileField()
28
A view handling this form will receive the file data in ``request.FILES``, which
29
is a dictionary containing a key for each ``FileField`` (or ``ImageField``, or
30
other ``FileField`` subclass) in the form. So the data from the above form would
31
be accessible as ``request.FILES['file']``.
33
Most of the time, you'll simply pass the file data from ``request`` into the
34
form as described in :ref:`binding-uploaded-files`. This would look
37
from django.http import HttpResponseRedirect
38
from django.shortcuts import render_to_response
40
# Imaginary function to handle an uploaded file.
41
from somewhere import handle_uploaded_file
43
def upload_file(request):
44
if request.method == 'POST':
45
form = UploadFileForm(request.POST, request.FILES)
47
handle_uploaded_file(request.FILES['file'])
48
return HttpResponseRedirect('/success/url/')
50
form = UploadFileForm()
51
return render_to_response('upload.html', {'form': form})
53
Notice that we have to pass ``request.FILES`` into the form's constructor; this
54
is how file data gets bound into a form.
56
Handling uploaded files
57
-----------------------
59
The final piece of the puzzle is handling the actual file data from
60
``request.FILES``. Each entry in this dictionary is an ``UploadedFile`` object
61
-- a simple wrapper around an uploaded file. You'll usually use one of these
62
methods to access the uploaded content:
64
``UploadedFile.read()``
65
Read the entire uploaded data from the file. Be careful with this
66
method: if the uploaded file is huge it can overwhelm your system if you
67
try to read it into memory. You'll probably want to use ``chunks()``
70
``UploadedFile.multiple_chunks()``
71
Returns ``True`` if the uploaded file is big enough to require
72
reading in multiple chunks. By default this will be any file
73
larger than 2.5 megabytes, but that's configurable; see below.
75
``UploadedFile.chunks()``
76
A generator returning chunks of the file. If ``multiple_chunks()`` is
77
``True``, you should use this method in a loop instead of ``read()``.
79
In practice, it's often easiest simply to use ``chunks()`` all the time;
80
see the example below.
83
The name of the uploaded file (e.g. ``my_file.txt``).
86
The size, in bytes, of the uploaded file.
88
There are a few other methods and attributes available on ``UploadedFile``
89
objects; see `UploadedFile objects`_ for a complete reference.
91
Putting it all together, here's a common way you might handle an uploaded file::
93
def handle_uploaded_file(f):
94
destination = open('some/file/name.txt', 'wb+')
95
for chunk in f.chunks():
96
destination.write(chunk)
98
Looping over ``UploadedFile.chunks()`` instead of using ``read()`` ensures that
99
large files don't overwhelm your system's memory.
101
Where uploaded data is stored
102
-----------------------------
104
Before you save uploaded files, the data needs to be stored somewhere.
106
By default, if an uploaded file is smaller than 2.5 megabytes, Django will hold
107
the entire contents of the upload in memory. This means that saving the file
108
involves only a read from memory and a write to disk and thus is very fast.
110
However, if an uploaded file is too large, Django will write the uploaded file
111
to a temporary file stored in your system's temporary directory. On a Unix-like
112
platform this means you can expect Django to generate a file called something
113
like ``/tmp/tmpzfp6I6.upload``. If an upload is large enough, you can watch this
114
file grow in size as Django streams the data onto disk.
116
These specifics -- 2.5 megabytes; ``/tmp``; etc. -- are simply "reasonable
117
defaults". Read on for details on how you can customize or completely replace
120
Changing upload handler behavior
121
--------------------------------
123
Three settings control Django's file upload behavior:
125
:setting:`FILE_UPLOAD_MAX_MEMORY_SIZE`
126
The maximum size, in bytes, for files that will be uploaded into memory.
127
Files larger than :setting:`FILE_UPLOAD_MAX_MEMORY_SIZE` will be
130
Defaults to 2.5 megabytes.
132
:setting:`FILE_UPLOAD_TEMP_DIR`
133
The directory where uploaded files larger than
134
:setting:`FILE_UPLOAD_TEMP_DIR` will be stored.
136
Defaults to your system's standard temporary directory (i.e. ``/tmp`` on
137
most Unix-like systems).
139
:setting:`FILE_UPLOAD_PERMISSIONS`
140
The numeric mode (i.e. ``0644``) to set newly uploaded files to. For
141
more information about what these modes mean, see the `documentation for
144
If this isn't given or is ``None``, you'll get operating-system
145
dependent behavior. On most platforms, temporary files will have a mode
146
of ``0600``, and files saved from memory will be saved using the
147
system's standard umask.
151
If you're not familiar with file modes, please note that the leading
152
``0`` is very important: it indicates an octal number, which is the
153
way that modes must be specified. If you try to use ``644``, you'll
154
get totally incorrect behavior.
156
**Always prefix the mode with a ``0``.**
158
:setting:`FILE_UPLOAD_HANDLERS`
159
The actual handlers for uploaded files. Changing this setting allows
160
complete customization -- even replacement -- of Django's upload
161
process. See `upload handlers`_, below, for details.
165
("django.core.files.uploadhandler.MemoryFileUploadHandler",
166
"django.core.files.uploadhandler.TemporaryFileUploadHandler",)
168
Which means "try to upload to memory first, then fall back to temporary
171
.. _documentation for os.chmod: http://docs.python.org/lib/os-file-dir.html
173
``UploadedFile`` objects
174
========================
176
.. class:: UploadedFile
178
In addition to those inherited from :class:`File`, all ``UploadedFile`` objects
179
define the following methods/attributes:
181
``UploadedFile.content_type``
182
The content-type header uploaded with the file (e.g. ``text/plain`` or
183
``application/pdf``). Like any data supplied by the user, you shouldn't
184
trust that the uploaded file is actually this type. You'll still need to
185
validate that the file contains the content that the content-type header
186
claims -- "trust but verify."
188
``UploadedFile.charset``
189
For ``text/*`` content-types, the character set (i.e. ``utf8``) supplied
190
by the browser. Again, "trust but verify" is the best policy here.
192
``UploadedFile.temporary_file_path()``
193
Only files uploaded onto disk will have this method; it returns the full
194
path to the temporary uploaded file.
198
Like regular Python files, you can read the file line-by-line simply by
199
iterating over the uploaded file:
201
.. code-block:: python
203
for line in uploadedfile:
204
do_something_with(line)
206
However, *unlike* standard Python files, :class:`UploadedFile` only
207
understands ``\n`` (also known as "Unix-style") line endings. If you know
208
that you need to handle uploaded files with different line endings, you'll
209
need to do so in your view.
214
When a user uploads a file, Django passes off the file data to an *upload
215
handler* -- a small class that handles file data as it gets uploaded. Upload
216
handlers are initially defined in the ``FILE_UPLOAD_HANDLERS`` setting, which
219
("django.core.files.uploadhandler.MemoryFileUploadHandler",
220
"django.core.files.uploadhandler.TemporaryFileUploadHandler",)
222
Together the ``MemoryFileUploadHandler`` and ``TemporaryFileUploadHandler``
223
provide Django's default file upload behavior of reading small files into memory
224
and large ones onto disk.
226
You can write custom handlers that customize how Django handles files. You
227
could, for example, use custom handlers to enforce user-level quotas, compress
228
data on the fly, render progress bars, and even send data to another storage
229
location directly without storing it locally.
231
Modifying upload handlers on the fly
232
------------------------------------
234
Sometimes particular views require different upload behavior. In these cases,
235
you can override upload handlers on a per-request basis by modifying
236
``request.upload_handlers``. By default, this list will contain the upload
237
handlers given by ``FILE_UPLOAD_HANDLERS``, but you can modify the list as you
238
would any other list.
240
For instance, suppose you've written a ``ProgressBarUploadHandler`` that
241
provides feedback on upload progress to some sort of AJAX widget. You'd add this
242
handler to your upload handlers like this::
244
request.upload_handlers.insert(0, ProgressBarUploadHandler())
246
You'd probably want to use ``list.insert()`` in this case (instead of
247
``append()``) because a progress bar handler would need to run *before* any
248
other handlers. Remember, the upload handlers are processed in order.
250
If you want to replace the upload handlers completely, you can just assign a new
253
request.upload_handlers = [ProgressBarUploadHandler()]
257
You can only modify upload handlers *before* accessing
258
``request.POST`` or ``request.FILES`` -- it doesn't make sense to
259
change upload handlers after upload handling has already
260
started. If you try to modify ``request.upload_handlers`` after
261
reading from ``request.POST`` or ``request.FILES`` Django will
264
Thus, you should always modify uploading handlers as early in your view as
267
Writing custom upload handlers
268
------------------------------
270
All file upload handlers should be subclasses of
271
``django.core.files.uploadhandler.FileUploadHandler``. You can define upload
272
handlers wherever you wish.
277
Custom file upload handlers **must** define the following methods:
279
``FileUploadHandler.receive_data_chunk(self, raw_data, start)``
280
Receives a "chunk" of data from the file upload.
282
``raw_data`` is a byte string containing the uploaded data.
284
``start`` is the position in the file where this ``raw_data`` chunk
287
The data you return will get fed into the subsequent upload handlers'
288
``receive_data_chunk`` methods. In this way, one handler can be a
289
"filter" for other handlers.
291
Return ``None`` from ``receive_data_chunk`` to sort-circuit remaining
292
upload handlers from getting this chunk.. This is useful if you're
293
storing the uploaded data yourself and don't want future handlers to
294
store a copy of the data.
296
If you raise a ``StopUpload`` or a ``SkipFile`` exception, the upload
297
will abort or the file will be completely skipped.
299
``FileUploadHandler.file_complete(self, file_size)``
300
Called when a file has finished uploading.
302
The handler should return an ``UploadedFile`` object that will be stored
303
in ``request.FILES``. Handlers may also return ``None`` to indicate that
304
the ``UploadedFile`` object should come from subsequent upload handlers.
309
Custom upload handlers may also define any of the following optional methods or
312
``FileUploadHandler.chunk_size``
313
Size, in bytes, of the "chunks" Django should store into memory and feed
314
into the handler. That is, this attribute controls the size of chunks
315
fed into ``FileUploadHandler.receive_data_chunk``.
317
For maximum performance the chunk sizes should be divisible by ``4`` and
318
should not exceed 2 GB (2\ :sup:`31` bytes) in size. When there are
319
multiple chunk sizes provided by multiple handlers, Django will use the
320
smallest chunk size defined by any handler.
322
The default is 64*2\ :sup:`10` bytes, or 64 KB.
324
``FileUploadHandler.new_file(self, field_name, file_name, content_type, content_length, charset)``
325
Callback signaling that a new file upload is starting. This is called
326
before any data has been fed to any upload handlers.
328
``field_name`` is a string name of the file ``<input>`` field.
330
``file_name`` is the unicode filename that was provided by the browser.
332
``content_type`` is the MIME type provided by the browser -- E.g.
335
``content_length`` is the length of the image given by the browser.
336
Sometimes this won't be provided and will be ``None``., ``None``
339
``charset`` is the character set (i.e. ``utf8``) given by the browser.
340
Like ``content_length``, this sometimes won't be provided.
342
This method may raise a ``StopFutureHandlers`` exception to prevent
343
future handlers from handling this file.
345
``FileUploadHandler.upload_complete(self)``
346
Callback signaling that the entire upload (all files) has completed.
348
``FileUploadHandler.handle_raw_input(self, input_data, META, content_length, boundary, encoding)``
349
Allows the handler to completely override the parsing of the raw
352
``input_data`` is a file-like object that supports ``read()``-ing.
354
``META`` is the same object as ``request.META``.
356
``content_length`` is the length of the data in ``input_data``. Don't
357
read more than ``content_length`` bytes from ``input_data``.
359
``boundary`` is the MIME boundary for this request.
361
``encoding`` is the encoding of the request.
363
Return ``None`` if you want upload handling to continue, or a tuple of
364
``(POST, FILES)`` if you want to return the new data structures suitable
365
for the request directly.