2008-07-01 23:10:51 +08:00
|
|
|
============
|
|
|
|
File Uploads
|
|
|
|
============
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
.. currentmodule:: django.core.files.uploadedfile
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2008-11-18 14:35:05 +08:00
|
|
|
When Django handles a file upload, the file data ends up placed in
|
2010-02-24 21:57:46 +08:00
|
|
|
:attr:`request.FILES <django.http.HttpRequest.FILES>` (for more on the
|
2010-08-20 03:27:44 +08:00
|
|
|
``request`` object see the documentation for :doc:`request and response objects
|
|
|
|
</ref/request-response>`). This document explains how files are stored on disk
|
2010-02-24 21:57:46 +08:00
|
|
|
and in memory, and how to customize the default behavior.
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2013-11-13 20:38:03 +08:00
|
|
|
.. warning::
|
|
|
|
|
|
|
|
There are security risks if you are accepting uploaded content from
|
|
|
|
untrusted users! See the security guide's topic on
|
|
|
|
:ref:`user-uploaded-content-security` for mitigation details.
|
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
Basic file uploads
|
|
|
|
==================
|
|
|
|
|
2010-02-24 21:57:46 +08:00
|
|
|
Consider a simple form containing a :class:`~django.forms.FileField`::
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2013-05-18 20:00:52 +08:00
|
|
|
# In forms.py...
|
2008-07-22 00:38:54 +08:00
|
|
|
from django import forms
|
2008-07-01 23:10:51 +08:00
|
|
|
|
|
|
|
class UploadFileForm(forms.Form):
|
|
|
|
title = forms.CharField(max_length=50)
|
|
|
|
file = forms.FileField()
|
2008-07-08 07:16:00 +08:00
|
|
|
|
2010-02-24 21:57:46 +08:00
|
|
|
A view handling this form will receive the file data in
|
|
|
|
:attr:`request.FILES <django.http.HttpRequest.FILES>`, which is a dictionary
|
|
|
|
containing a key for each :class:`~django.forms.FileField` (or
|
|
|
|
:class:`~django.forms.ImageField`, or other :class:`~django.forms.FileField`
|
|
|
|
subclass) in the form. So the data from the above form would
|
2008-07-01 23:10:51 +08:00
|
|
|
be accessible as ``request.FILES['file']``.
|
|
|
|
|
2010-02-24 21:57:46 +08:00
|
|
|
Note that :attr:`request.FILES <django.http.HttpRequest.FILES>` will only
|
|
|
|
contain data if the request method was ``POST`` and the ``<form>`` that posted
|
|
|
|
the request has the attribute ``enctype="multipart/form-data"``. Otherwise,
|
|
|
|
``request.FILES`` will be empty.
|
2009-05-18 02:45:25 +08:00
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
Most of the time, you'll simply pass the file data from ``request`` into the
|
2008-08-24 06:25:40 +08:00
|
|
|
form as described in :ref:`binding-uploaded-files`. This would look
|
2008-07-01 23:10:51 +08:00
|
|
|
something like::
|
|
|
|
|
|
|
|
from django.http import HttpResponseRedirect
|
|
|
|
from django.shortcuts import render_to_response
|
2013-05-18 20:00:52 +08:00
|
|
|
from .forms import UploadFileForm
|
2008-07-01 23:10:51 +08:00
|
|
|
|
|
|
|
# Imaginary function to handle an uploaded file.
|
|
|
|
from somewhere import handle_uploaded_file
|
|
|
|
|
|
|
|
def upload_file(request):
|
|
|
|
if request.method == 'POST':
|
|
|
|
form = UploadFileForm(request.POST, request.FILES)
|
|
|
|
if form.is_valid():
|
|
|
|
handle_uploaded_file(request.FILES['file'])
|
|
|
|
return HttpResponseRedirect('/success/url/')
|
|
|
|
else:
|
|
|
|
form = UploadFileForm()
|
|
|
|
return render_to_response('upload.html', {'form': form})
|
|
|
|
|
2010-02-24 21:57:46 +08:00
|
|
|
Notice that we have to pass :attr:`request.FILES <django.http.HttpRequest.FILES>`
|
|
|
|
into the form's constructor; this is how file data gets bound into a form.
|
2008-07-01 23:10:51 +08:00
|
|
|
|
|
|
|
Handling uploaded files
|
|
|
|
-----------------------
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
.. class:: UploadedFile
|
|
|
|
|
|
|
|
The final piece of the puzzle is handling the actual file data from
|
|
|
|
:attr:`request.FILES <django.http.HttpRequest.FILES>`. Each entry in this
|
|
|
|
dictionary is an ``UploadedFile`` object -- a simple wrapper around an uploaded
|
|
|
|
file. You'll usually use one of these methods to access the uploaded content:
|
|
|
|
|
|
|
|
.. method:: read()
|
2008-07-01 23:10:51 +08:00
|
|
|
|
|
|
|
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
|
2008-07-08 07:16:00 +08:00
|
|
|
try to read it into memory. You'll probably want to use ``chunks()``
|
2008-07-01 23:10:51 +08:00
|
|
|
instead; see below.
|
2008-07-08 07:16:00 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
.. method:: multiple_chunks()
|
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
Returns ``True`` if 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.
|
2008-07-08 07:16:00 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
.. method:: chunks()
|
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
A generator returning chunks of the file. If ``multiple_chunks()`` is
|
|
|
|
``True``, you should use this method in a loop instead of ``read()``.
|
2008-07-08 07:16:00 +08:00
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
In practice, it's often easiest simply to use ``chunks()`` all the time;
|
|
|
|
see the example below.
|
2008-07-08 07:16:00 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
.. attribute:: name
|
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
The name of the uploaded file (e.g. ``my_file.txt``).
|
2008-07-08 07:16:00 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
.. attribute:: size
|
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
The size, in bytes, of the uploaded file.
|
2008-07-08 07:16:00 +08:00
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
There are a few other methods and attributes available on ``UploadedFile``
|
|
|
|
objects; see `UploadedFile objects`_ for a complete reference.
|
|
|
|
|
|
|
|
Putting it all together, here's a common way you might handle an uploaded file::
|
2008-07-08 07:16:00 +08:00
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
def handle_uploaded_file(f):
|
2012-05-05 20:01:38 +08:00
|
|
|
with open('some/file/name.txt', 'wb+') as destination:
|
|
|
|
for chunk in f.chunks():
|
|
|
|
destination.write(chunk)
|
2008-07-01 23:10:51 +08:00
|
|
|
|
|
|
|
Looping over ``UploadedFile.chunks()`` instead of using ``read()`` ensures that
|
|
|
|
large files don't overwhelm your system's memory.
|
|
|
|
|
|
|
|
Where uploaded data is stored
|
|
|
|
-----------------------------
|
|
|
|
|
|
|
|
Before you save uploaded files, the data needs to be stored somewhere.
|
|
|
|
|
|
|
|
By default, if an uploaded file is smaller than 2.5 megabytes, Django will hold
|
|
|
|
the entire contents of the upload in memory. This means that saving the file
|
|
|
|
involves only a read from memory and a write to disk and thus is very fast.
|
|
|
|
|
|
|
|
However, if an uploaded file is too large, Django will write the uploaded file
|
|
|
|
to a temporary file stored in your system's temporary directory. On a Unix-like
|
|
|
|
platform this means you can expect Django to generate a file called something
|
|
|
|
like ``/tmp/tmpzfp6I6.upload``. If an upload is large enough, you can watch this
|
|
|
|
file grow in size as Django streams the data onto disk.
|
|
|
|
|
|
|
|
These specifics -- 2.5 megabytes; ``/tmp``; etc. -- are simply "reasonable
|
|
|
|
defaults". Read on for details on how you can customize or completely replace
|
|
|
|
upload behavior.
|
|
|
|
|
|
|
|
Changing upload handler behavior
|
|
|
|
--------------------------------
|
|
|
|
|
2013-05-13 19:38:53 +08:00
|
|
|
There are a few settings which control Django's file upload behavior:
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
:setting:`FILE_UPLOAD_MAX_MEMORY_SIZE`
|
|
|
|
The maximum size, in bytes, for files that will be uploaded into memory.
|
|
|
|
Files larger than :setting:`FILE_UPLOAD_MAX_MEMORY_SIZE` will be
|
|
|
|
streamed to disk.
|
2008-07-08 07:16:00 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Defaults to 2.5 megabytes.
|
2008-07-08 07:16:00 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
:setting:`FILE_UPLOAD_TEMP_DIR`
|
|
|
|
The directory where uploaded files larger than
|
|
|
|
:setting:`FILE_UPLOAD_MAX_MEMORY_SIZE` will be stored.
|
2008-07-08 07:16:00 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Defaults to your system's standard temporary directory (i.e. ``/tmp`` on
|
|
|
|
most Unix-like systems).
|
2009-12-26 14:37:26 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
:setting:`FILE_UPLOAD_PERMISSIONS`
|
|
|
|
The numeric mode (i.e. ``0644``) to set newly uploaded files to. For
|
|
|
|
more information about what these modes mean, see the documentation for
|
|
|
|
:func:`os.chmod`.
|
2009-12-26 14:37:26 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
If this isn't given or is ``None``, you'll get operating-system
|
|
|
|
dependent behavior. On most platforms, temporary files will have a mode
|
|
|
|
of ``0600``, and files saved from memory will be saved using the
|
|
|
|
system's standard umask.
|
2009-12-26 14:37:26 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
.. warning::
|
2009-12-26 14:37:26 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
If you're not familiar with file modes, please note that the leading
|
|
|
|
``0`` is very important: it indicates an octal number, which is the
|
|
|
|
way that modes must be specified. If you try to use ``644``, you'll
|
|
|
|
get totally incorrect behavior.
|
2008-11-03 04:43:20 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
**Always prefix the mode with a 0.**
|
2008-08-28 06:21:14 +08:00
|
|
|
|
2013-05-13 19:38:53 +08:00
|
|
|
:setting:`FILE_UPLOAD_DIRECTORY_PERMISSIONS`
|
|
|
|
The numeric mode to apply to directories created in the process of
|
|
|
|
uploading files. This value mirrors the functionality and caveats of
|
|
|
|
the :setting:`FILE_UPLOAD_PERMISSIONS` setting.
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
:setting:`FILE_UPLOAD_HANDLERS`
|
|
|
|
The actual handlers for uploaded files. Changing this setting allows
|
|
|
|
complete customization -- even replacement -- of Django's upload
|
|
|
|
process. See `upload handlers`_, below, for details.
|
2008-07-08 07:16:00 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Defaults to::
|
2008-07-08 07:16:00 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
("django.core.files.uploadhandler.MemoryFileUploadHandler",
|
|
|
|
"django.core.files.uploadhandler.TemporaryFileUploadHandler",)
|
2008-07-08 07:16:00 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Which means "try to upload to memory first, then fall back to temporary
|
|
|
|
files."
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2012-08-11 04:19:20 +08:00
|
|
|
Handling uploaded files with a model
|
|
|
|
------------------------------------
|
|
|
|
|
|
|
|
If you're saving a file on a :class:`~django.db.models.Model` with a
|
|
|
|
:class:`~django.db.models.FileField`, using a :class:`~django.forms.ModelForm`
|
2012-09-02 03:38:44 +08:00
|
|
|
makes this process much easier. The file object will be saved to the location
|
|
|
|
specified by the :attr:`~django.db.models.FileField.upload_to` argument of the
|
|
|
|
corresponding :class:`~django.db.models.FileField` when calling
|
2012-08-11 04:19:20 +08:00
|
|
|
``form.save()``::
|
|
|
|
|
|
|
|
from django.http import HttpResponseRedirect
|
|
|
|
from django.shortcuts import render
|
|
|
|
from .forms import ModelFormWithFileField
|
|
|
|
|
|
|
|
def upload_file(request):
|
|
|
|
if request.method == 'POST':
|
|
|
|
form = ModelFormWithFileField(request.POST, request.FILES)
|
|
|
|
if form.is_valid():
|
|
|
|
# file is saved
|
|
|
|
form.save()
|
|
|
|
return HttpResponseRedirect('/success/url/')
|
|
|
|
else:
|
|
|
|
form = ModelFormWithFileField()
|
2013-01-11 05:17:52 +08:00
|
|
|
return render(request, 'upload.html', {'form': form})
|
2012-08-11 04:19:20 +08:00
|
|
|
|
|
|
|
If you are constructing an object manually, you can simply assign the file
|
|
|
|
object from :attr:`request.FILES <django.http.HttpRequest.FILES>` to the file
|
|
|
|
field in the model::
|
|
|
|
|
|
|
|
from django.http import HttpResponseRedirect
|
|
|
|
from django.shortcuts import render
|
|
|
|
from .forms import UploadFileForm
|
|
|
|
from .models import ModelWithFileField
|
|
|
|
|
|
|
|
def upload_file(request):
|
|
|
|
if request.method == 'POST':
|
|
|
|
form = UploadFileForm(request.POST, request.FILES)
|
|
|
|
if form.is_valid():
|
|
|
|
instance = ModelWithFileField(file_field=request.FILES['file'])
|
|
|
|
instance.save()
|
|
|
|
return HttpResponseRedirect('/success/url/')
|
|
|
|
else:
|
|
|
|
form = UploadFileForm()
|
2013-01-11 05:17:52 +08:00
|
|
|
return render(request, 'upload.html', {'form': form})
|
2012-08-11 04:19:20 +08:00
|
|
|
|
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
``UploadedFile`` objects
|
|
|
|
========================
|
|
|
|
|
2013-01-01 21:12:42 +08:00
|
|
|
In addition to those inherited from :class:`~django.core.files.File`, all
|
|
|
|
``UploadedFile`` objects define the following methods/attributes:
|
2008-07-08 07:16:00 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
.. attribute:: UploadedFile.content_type
|
|
|
|
|
2011-09-05 05:17:30 +08:00
|
|
|
The content-type header uploaded with the file (e.g. :mimetype:`text/plain`
|
|
|
|
or :mimetype:`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."
|
2011-02-16 09:56:53 +08:00
|
|
|
|
2013-04-20 01:20:23 +08:00
|
|
|
.. attribute:: UploadedFile.content_type_extra
|
|
|
|
|
|
|
|
.. versionadded:: 1.7
|
|
|
|
|
|
|
|
A dictionary containing extra parameters passed to the ``content-type``
|
|
|
|
header. 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`_ section 5.3).
|
|
|
|
|
|
|
|
.. _RFC 2388: http://www.ietf.org/rfc/rfc2388.txt
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
.. attribute:: UploadedFile.charset
|
|
|
|
|
2011-09-05 05:17:30 +08:00
|
|
|
For :mimetype:`text/*` content-types, the character set (i.e. ``utf8``)
|
|
|
|
supplied by the browser. Again, "trust but verify" is the best policy here.
|
2008-07-08 07:16:00 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
.. attribute:: UploadedFile.temporary_file_path()
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Only files uploaded onto disk will have this method; it returns the full
|
|
|
|
path to the temporary uploaded file.
|
2009-12-26 14:37:26 +08:00
|
|
|
|
2008-08-29 05:39:17 +08:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
Like regular Python files, you can read the file line-by-line simply by
|
|
|
|
iterating over the uploaded file:
|
2009-12-26 14:37:26 +08:00
|
|
|
|
2008-08-29 05:39:17 +08:00
|
|
|
.. code-block:: python
|
2009-12-26 14:37:26 +08:00
|
|
|
|
2008-08-29 05:39:17 +08:00
|
|
|
for line in uploadedfile:
|
|
|
|
do_something_with(line)
|
2009-12-26 14:37:26 +08:00
|
|
|
|
2008-08-29 05:39:17 +08:00
|
|
|
However, *unlike* standard Python files, :class:`UploadedFile` only
|
|
|
|
understands ``\n`` (also known as "Unix-style") line endings. If you know
|
|
|
|
that you need to handle uploaded files with different line endings, you'll
|
|
|
|
need to do so in your view.
|
2008-07-01 23:10:51 +08:00
|
|
|
|
|
|
|
Upload Handlers
|
|
|
|
===============
|
|
|
|
|
|
|
|
When a user uploads a file, Django passes off the file data to an *upload
|
|
|
|
handler* -- a small class that handles file data as it gets uploaded. Upload
|
2011-05-30 01:41:04 +08:00
|
|
|
handlers are initially defined in the :setting:`FILE_UPLOAD_HANDLERS` setting,
|
|
|
|
which defaults to::
|
2008-07-01 23:10:51 +08:00
|
|
|
|
|
|
|
("django.core.files.uploadhandler.MemoryFileUploadHandler",
|
|
|
|
"django.core.files.uploadhandler.TemporaryFileUploadHandler",)
|
|
|
|
|
|
|
|
Together the ``MemoryFileUploadHandler`` and ``TemporaryFileUploadHandler``
|
|
|
|
provide Django's default file upload behavior of reading small files into memory
|
|
|
|
and large ones onto disk.
|
|
|
|
|
|
|
|
You can write custom handlers that customize how Django handles files. You
|
|
|
|
could, for example, use custom handlers to enforce user-level quotas, compress
|
|
|
|
data on the fly, render progress bars, and even send data to another storage
|
|
|
|
location directly without storing it locally.
|
|
|
|
|
2011-09-10 01:20:16 +08:00
|
|
|
.. _modifying_upload_handlers_on_the_fly:
|
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
Modifying upload handlers on the fly
|
|
|
|
------------------------------------
|
|
|
|
|
|
|
|
Sometimes particular views require different upload behavior. In these cases,
|
|
|
|
you can override upload handlers on a per-request basis by modifying
|
|
|
|
``request.upload_handlers``. By default, this list will contain the upload
|
2011-05-30 01:41:04 +08:00
|
|
|
handlers given by :setting:`FILE_UPLOAD_HANDLERS`, but you can modify the list
|
|
|
|
as you would any other list.
|
2008-07-01 23:10:51 +08:00
|
|
|
|
|
|
|
For instance, suppose you've written a ``ProgressBarUploadHandler`` that
|
|
|
|
provides feedback on upload progress to some sort of AJAX widget. You'd add this
|
2008-08-24 06:25:40 +08:00
|
|
|
handler to your upload handlers like this::
|
2008-07-01 23:10:51 +08:00
|
|
|
|
|
|
|
request.upload_handlers.insert(0, ProgressBarUploadHandler())
|
|
|
|
|
|
|
|
You'd probably want to use ``list.insert()`` in this case (instead of
|
|
|
|
``append()``) because a progress bar handler would need to run *before* any
|
|
|
|
other handlers. Remember, the upload handlers are processed in order.
|
|
|
|
|
|
|
|
If you want to replace the upload handlers completely, you can just assign a new
|
|
|
|
list::
|
|
|
|
|
|
|
|
request.upload_handlers = [ProgressBarUploadHandler()]
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
2008-08-31 18:23:36 +08:00
|
|
|
You can only modify upload handlers *before* accessing
|
|
|
|
``request.POST`` or ``request.FILES`` -- it doesn't make sense to
|
|
|
|
change upload handlers after upload handling has already
|
|
|
|
started. If you try to modify ``request.upload_handlers`` after
|
|
|
|
reading from ``request.POST`` or ``request.FILES`` Django will
|
|
|
|
throw an error.
|
2008-07-01 23:10:51 +08:00
|
|
|
|
|
|
|
Thus, you should always modify uploading handlers as early in your view as
|
|
|
|
possible.
|
|
|
|
|
2010-09-30 00:35:34 +08:00
|
|
|
Also, ``request.POST`` is accessed by
|
|
|
|
:class:`~django.middleware.csrf.CsrfViewMiddleware` which is enabled by
|
2011-08-07 04:34:04 +08:00
|
|
|
default. This means you will need to use
|
2010-09-30 00:35:34 +08:00
|
|
|
:func:`~django.views.decorators.csrf.csrf_exempt` on your view to allow you
|
2011-08-07 04:34:04 +08:00
|
|
|
to change the upload handlers. You will then need to use
|
|
|
|
:func:`~django.views.decorators.csrf.csrf_protect` on the function that
|
|
|
|
actually processes the request. Note that this means that the handlers may
|
|
|
|
start receiving the file upload before the CSRF checks have been done.
|
|
|
|
Example code:
|
2010-09-30 00:35:34 +08:00
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from django.views.decorators.csrf import csrf_exempt, csrf_protect
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
def upload_file_view(request):
|
|
|
|
request.upload_handlers.insert(0, ProgressBarUploadHandler())
|
|
|
|
return _upload_file_view(request)
|
|
|
|
|
|
|
|
@csrf_protect
|
|
|
|
def _upload_file_view(request):
|
|
|
|
... # Process request
|
|
|
|
|
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
Writing custom upload handlers
|
|
|
|
------------------------------
|
|
|
|
|
2013-04-20 01:20:23 +08:00
|
|
|
.. currentmodule:: django.core.files.uploadhandler
|
|
|
|
|
|
|
|
.. class:: FileUploadHandler
|
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
All file upload handlers should be subclasses of
|
|
|
|
``django.core.files.uploadhandler.FileUploadHandler``. You can define upload
|
|
|
|
handlers wherever you wish.
|
|
|
|
|
|
|
|
Required methods
|
|
|
|
~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Custom file upload handlers **must** define the following methods:
|
|
|
|
|
2013-04-20 01:20:23 +08:00
|
|
|
.. method:: FileUploadHandler.receive_data_chunk(self, raw_data, start)
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Receives a "chunk" of data from the file upload.
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
``raw_data`` is a byte string containing the uploaded data.
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
``start`` is the position in the file where this ``raw_data`` chunk
|
|
|
|
begins.
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
The data you return will get fed into the subsequent upload handlers'
|
|
|
|
``receive_data_chunk`` methods. In this way, one handler can be a
|
|
|
|
"filter" for other handlers.
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2013-06-24 11:59:03 +08:00
|
|
|
Return ``None`` from ``receive_data_chunk`` to short-circuit remaining
|
|
|
|
upload handlers from getting this chunk. This is useful if you're
|
2011-10-14 08:12:01 +08:00
|
|
|
storing the uploaded data yourself and don't want future handlers to
|
|
|
|
store a copy of the data.
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
If you raise a ``StopUpload`` or a ``SkipFile`` exception, the upload
|
|
|
|
will abort or the file will be completely skipped.
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2013-04-20 01:20:23 +08:00
|
|
|
.. method:: FileUploadHandler.file_complete(self, file_size)
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Called when a file has finished uploading.
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
The handler should return an ``UploadedFile`` object that will be stored
|
|
|
|
in ``request.FILES``. Handlers may also return ``None`` to indicate that
|
|
|
|
the ``UploadedFile`` object should come from subsequent upload handlers.
|
2008-07-01 23:10:51 +08:00
|
|
|
|
|
|
|
Optional methods
|
|
|
|
~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Custom upload handlers may also define any of the following optional methods or
|
|
|
|
attributes:
|
|
|
|
|
2013-04-20 01:20:23 +08:00
|
|
|
.. attribute:: FileUploadHandler.chunk_size
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Size, in bytes, of the "chunks" Django should store into memory and feed
|
|
|
|
into the handler. That is, this attribute controls the size of chunks
|
|
|
|
fed into ``FileUploadHandler.receive_data_chunk``.
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
For maximum performance the chunk sizes should be divisible by ``4`` and
|
|
|
|
should not exceed 2 GB (2\ :sup:`31` bytes) in size. When there are
|
|
|
|
multiple chunk sizes provided by multiple handlers, Django will use the
|
|
|
|
smallest chunk size defined by any handler.
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
The default is 64*2\ :sup:`10` bytes, or 64 KB.
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2013-04-20 01:20:23 +08:00
|
|
|
.. method:: FileUploadHandler.new_file(self, field_name, file_name, content_type, content_length, charset, content_type_extra)
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Callback signaling that a new file upload is starting. This is called
|
|
|
|
before any data has been fed to any upload handlers.
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
``field_name`` is a string name of the file ``<input>`` field.
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
``file_name`` is the unicode filename that was provided by the browser.
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
``content_type`` is the MIME type provided by the browser -- E.g.
|
|
|
|
``'image/jpeg'``.
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
``content_length`` is the length of the image given by the browser.
|
|
|
|
Sometimes this won't be provided and will be ``None``.
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
``charset`` is the character set (i.e. ``utf8``) given by the browser.
|
|
|
|
Like ``content_length``, this sometimes won't be provided.
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2013-04-20 01:20:23 +08:00
|
|
|
``content_type_extra`` is extra information about the file from the
|
|
|
|
``content-type`` header. See :attr:`UploadedFile.content_type_extra
|
|
|
|
<django.core.files.uploadedfile.UploadedFile.content_type_extra>`.
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
This method may raise a ``StopFutureHandlers`` exception to prevent
|
|
|
|
future handlers from handling this file.
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2013-04-20 01:20:23 +08:00
|
|
|
.. versionadded:: 1.7
|
|
|
|
|
|
|
|
The ``content_type_extra`` parameter was added.
|
|
|
|
|
|
|
|
.. method:: FileUploadHandler.upload_complete(self)
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Callback signaling that the entire upload (all files) has completed.
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2013-04-20 01:20:23 +08:00
|
|
|
.. method:: FileUploadHandler.handle_raw_input(self, input_data, META, content_length, boundary, encoding)
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Allows the handler to completely override the parsing of the raw
|
|
|
|
HTTP input.
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
``input_data`` is a file-like object that supports ``read()``-ing.
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
``META`` is the same object as ``request.META``.
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
``content_length`` is the length of the data in ``input_data``. Don't
|
|
|
|
read more than ``content_length`` bytes from ``input_data``.
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
``boundary`` is the MIME boundary for this request.
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
``encoding`` is the encoding of the request.
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Return ``None`` if you want upload handling to continue, or a tuple of
|
|
|
|
``(POST, FILES)`` if you want to return the new data structures suitable
|
|
|
|
for the request directly.
|