file-uploads.txt 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. ============
  2. File Uploads
  3. ============
  4. .. currentmodule:: django.core.files.uploadedfile
  5. When Django handles a file upload, the file data ends up placed in
  6. :attr:`request.FILES <django.http.HttpRequest.FILES>` (for more on the
  7. ``request`` object see the documentation for :doc:`request and response objects
  8. </ref/request-response>`). This document explains how files are stored on disk
  9. and in memory, and how to customize the default behavior.
  10. Basic file uploads
  11. ==================
  12. Consider a simple form containing a :class:`~django.forms.FileField`::
  13. # In forms.py...
  14. from django import forms
  15. class UploadFileForm(forms.Form):
  16. title = forms.CharField(max_length=50)
  17. file = forms.FileField()
  18. A view handling this form will receive the file data in
  19. :attr:`request.FILES <django.http.HttpRequest.FILES>`, which is a dictionary
  20. containing a key for each :class:`~django.forms.FileField` (or
  21. :class:`~django.forms.ImageField`, or other :class:`~django.forms.FileField`
  22. subclass) in the form. So the data from the above form would
  23. be accessible as ``request.FILES['file']``.
  24. Note that :attr:`request.FILES <django.http.HttpRequest.FILES>` will only
  25. contain data if the request method was ``POST`` and the ``<form>`` that posted
  26. the request has the attribute ``enctype="multipart/form-data"``. Otherwise,
  27. ``request.FILES`` will be empty.
  28. Most of the time, you'll simply pass the file data from ``request`` into the
  29. form as described in :ref:`binding-uploaded-files`. This would look
  30. something like::
  31. from django.http import HttpResponseRedirect
  32. from django.shortcuts import render_to_response
  33. from .forms import UploadFileForm
  34. # Imaginary function to handle an uploaded file.
  35. from somewhere import handle_uploaded_file
  36. def upload_file(request):
  37. if request.method == 'POST':
  38. form = UploadFileForm(request.POST, request.FILES)
  39. if form.is_valid():
  40. handle_uploaded_file(request.FILES['file'])
  41. return HttpResponseRedirect('/success/url/')
  42. else:
  43. form = UploadFileForm()
  44. return render_to_response('upload.html', {'form': form})
  45. Notice that we have to pass :attr:`request.FILES <django.http.HttpRequest.FILES>`
  46. into the form's constructor; this is how file data gets bound into a form.
  47. Handling uploaded files
  48. -----------------------
  49. .. class:: UploadedFile
  50. The final piece of the puzzle is handling the actual file data from
  51. :attr:`request.FILES <django.http.HttpRequest.FILES>`. Each entry in this
  52. dictionary is an ``UploadedFile`` object -- a simple wrapper around an uploaded
  53. file. You'll usually use one of these methods to access the uploaded content:
  54. .. method:: read()
  55. Read the entire uploaded data from the file. Be careful with this
  56. method: if the uploaded file is huge it can overwhelm your system if you
  57. try to read it into memory. You'll probably want to use ``chunks()``
  58. instead; see below.
  59. .. method:: multiple_chunks()
  60. Returns ``True`` if the uploaded file is big enough to require
  61. reading in multiple chunks. By default this will be any file
  62. larger than 2.5 megabytes, but that's configurable; see below.
  63. .. method:: chunks()
  64. A generator returning chunks of the file. If ``multiple_chunks()`` is
  65. ``True``, you should use this method in a loop instead of ``read()``.
  66. In practice, it's often easiest simply to use ``chunks()`` all the time;
  67. see the example below.
  68. .. attribute:: name
  69. The name of the uploaded file (e.g. ``my_file.txt``).
  70. .. attribute:: size
  71. The size, in bytes, of the uploaded file.
  72. There are a few other methods and attributes available on ``UploadedFile``
  73. objects; see `UploadedFile objects`_ for a complete reference.
  74. Putting it all together, here's a common way you might handle an uploaded file::
  75. def handle_uploaded_file(f):
  76. with open('some/file/name.txt', 'wb+') as destination:
  77. for chunk in f.chunks():
  78. destination.write(chunk)
  79. Looping over ``UploadedFile.chunks()`` instead of using ``read()`` ensures that
  80. large files don't overwhelm your system's memory.
  81. Where uploaded data is stored
  82. -----------------------------
  83. Before you save uploaded files, the data needs to be stored somewhere.
  84. By default, if an uploaded file is smaller than 2.5 megabytes, Django will hold
  85. the entire contents of the upload in memory. This means that saving the file
  86. involves only a read from memory and a write to disk and thus is very fast.
  87. However, if an uploaded file is too large, Django will write the uploaded file
  88. to a temporary file stored in your system's temporary directory. On a Unix-like
  89. platform this means you can expect Django to generate a file called something
  90. like ``/tmp/tmpzfp6I6.upload``. If an upload is large enough, you can watch this
  91. file grow in size as Django streams the data onto disk.
  92. These specifics -- 2.5 megabytes; ``/tmp``; etc. -- are simply "reasonable
  93. defaults". Read on for details on how you can customize or completely replace
  94. upload behavior.
  95. Changing upload handler behavior
  96. --------------------------------
  97. There are a few settings which control Django's file upload behavior:
  98. :setting:`FILE_UPLOAD_MAX_MEMORY_SIZE`
  99. The maximum size, in bytes, for files that will be uploaded into memory.
  100. Files larger than :setting:`FILE_UPLOAD_MAX_MEMORY_SIZE` will be
  101. streamed to disk.
  102. Defaults to 2.5 megabytes.
  103. :setting:`FILE_UPLOAD_TEMP_DIR`
  104. The directory where uploaded files larger than
  105. :setting:`FILE_UPLOAD_MAX_MEMORY_SIZE` will be stored.
  106. Defaults to your system's standard temporary directory (i.e. ``/tmp`` on
  107. most Unix-like systems).
  108. :setting:`FILE_UPLOAD_PERMISSIONS`
  109. The numeric mode (i.e. ``0644``) to set newly uploaded files to. For
  110. more information about what these modes mean, see the documentation for
  111. :func:`os.chmod`.
  112. If this isn't given or is ``None``, you'll get operating-system
  113. dependent behavior. On most platforms, temporary files will have a mode
  114. of ``0600``, and files saved from memory will be saved using the
  115. system's standard umask.
  116. .. warning::
  117. If you're not familiar with file modes, please note that the leading
  118. ``0`` is very important: it indicates an octal number, which is the
  119. way that modes must be specified. If you try to use ``644``, you'll
  120. get totally incorrect behavior.
  121. **Always prefix the mode with a 0.**
  122. :setting:`FILE_UPLOAD_DIRECTORY_PERMISSIONS`
  123. The numeric mode to apply to directories created in the process of
  124. uploading files. This value mirrors the functionality and caveats of
  125. the :setting:`FILE_UPLOAD_PERMISSIONS` setting.
  126. :setting:`FILE_UPLOAD_HANDLERS`
  127. The actual handlers for uploaded files. Changing this setting allows
  128. complete customization -- even replacement -- of Django's upload
  129. process. See `upload handlers`_, below, for details.
  130. Defaults to::
  131. ("django.core.files.uploadhandler.MemoryFileUploadHandler",
  132. "django.core.files.uploadhandler.TemporaryFileUploadHandler",)
  133. Which means "try to upload to memory first, then fall back to temporary
  134. files."
  135. Handling uploaded files with a model
  136. ------------------------------------
  137. If you're saving a file on a :class:`~django.db.models.Model` with a
  138. :class:`~django.db.models.FileField`, using a :class:`~django.forms.ModelForm`
  139. makes this process much easier. The file object will be saved to the location
  140. specified by the :attr:`~django.db.models.FileField.upload_to` argument of the
  141. corresponding :class:`~django.db.models.FileField` when calling
  142. ``form.save()``::
  143. from django.http import HttpResponseRedirect
  144. from django.shortcuts import render
  145. from .forms import ModelFormWithFileField
  146. def upload_file(request):
  147. if request.method == 'POST':
  148. form = ModelFormWithFileField(request.POST, request.FILES)
  149. if form.is_valid():
  150. # file is saved
  151. form.save()
  152. return HttpResponseRedirect('/success/url/')
  153. else:
  154. form = ModelFormWithFileField()
  155. return render(request, 'upload.html', {'form': form})
  156. If you are constructing an object manually, you can simply assign the file
  157. object from :attr:`request.FILES <django.http.HttpRequest.FILES>` to the file
  158. field in the model::
  159. from django.http import HttpResponseRedirect
  160. from django.shortcuts import render
  161. from .forms import UploadFileForm
  162. from .models import ModelWithFileField
  163. def upload_file(request):
  164. if request.method == 'POST':
  165. form = UploadFileForm(request.POST, request.FILES)
  166. if form.is_valid():
  167. instance = ModelWithFileField(file_field=request.FILES['file'])
  168. instance.save()
  169. return HttpResponseRedirect('/success/url/')
  170. else:
  171. form = UploadFileForm()
  172. return render(request, 'upload.html', {'form': form})
  173. ``UploadedFile`` objects
  174. ========================
  175. In addition to those inherited from :class:`~django.core.files.File`, all
  176. ``UploadedFile`` objects define the following methods/attributes:
  177. .. attribute:: UploadedFile.content_type
  178. The content-type header uploaded with the file (e.g. :mimetype:`text/plain`
  179. or :mimetype:`application/pdf`). Like any data supplied by the user, you
  180. shouldn't trust that the uploaded file is actually this type. You'll still
  181. need to validate that the file contains the content that the content-type
  182. header claims -- "trust but verify."
  183. .. attribute:: UploadedFile.content_type_extra
  184. .. versionadded:: 1.7
  185. A dictionary containing extra parameters passed to the ``content-type``
  186. header. This is typically provided by services, such as Google App Engine,
  187. that intercept and handle file uploads on your behalf. As a result your
  188. handler may not receive the uploaded file content, but instead a URL or
  189. other pointer to the file. (see `RFC 2388`_ section 5.3).
  190. .. _RFC 2388: http://www.ietf.org/rfc/rfc2388.txt
  191. .. attribute:: UploadedFile.charset
  192. For :mimetype:`text/*` content-types, the character set (i.e. ``utf8``)
  193. supplied by the browser. Again, "trust but verify" is the best policy here.
  194. .. attribute:: UploadedFile.temporary_file_path()
  195. Only files uploaded onto disk will have this method; it returns the full
  196. path to the temporary uploaded file.
  197. .. note::
  198. Like regular Python files, you can read the file line-by-line simply by
  199. iterating over the uploaded file:
  200. .. code-block:: python
  201. for line in uploadedfile:
  202. do_something_with(line)
  203. However, *unlike* standard Python files, :class:`UploadedFile` only
  204. understands ``\n`` (also known as "Unix-style") line endings. If you know
  205. that you need to handle uploaded files with different line endings, you'll
  206. need to do so in your view.
  207. Upload Handlers
  208. ===============
  209. When a user uploads a file, Django passes off the file data to an *upload
  210. handler* -- a small class that handles file data as it gets uploaded. Upload
  211. handlers are initially defined in the :setting:`FILE_UPLOAD_HANDLERS` setting,
  212. which defaults to::
  213. ("django.core.files.uploadhandler.MemoryFileUploadHandler",
  214. "django.core.files.uploadhandler.TemporaryFileUploadHandler",)
  215. Together the ``MemoryFileUploadHandler`` and ``TemporaryFileUploadHandler``
  216. provide Django's default file upload behavior of reading small files into memory
  217. and large ones onto disk.
  218. You can write custom handlers that customize how Django handles files. You
  219. could, for example, use custom handlers to enforce user-level quotas, compress
  220. data on the fly, render progress bars, and even send data to another storage
  221. location directly without storing it locally.
  222. .. _modifying_upload_handlers_on_the_fly:
  223. Modifying upload handlers on the fly
  224. ------------------------------------
  225. Sometimes particular views require different upload behavior. In these cases,
  226. you can override upload handlers on a per-request basis by modifying
  227. ``request.upload_handlers``. By default, this list will contain the upload
  228. handlers given by :setting:`FILE_UPLOAD_HANDLERS`, but you can modify the list
  229. as you would any other list.
  230. For instance, suppose you've written a ``ProgressBarUploadHandler`` that
  231. provides feedback on upload progress to some sort of AJAX widget. You'd add this
  232. handler to your upload handlers like this::
  233. request.upload_handlers.insert(0, ProgressBarUploadHandler())
  234. You'd probably want to use ``list.insert()`` in this case (instead of
  235. ``append()``) because a progress bar handler would need to run *before* any
  236. other handlers. Remember, the upload handlers are processed in order.
  237. If you want to replace the upload handlers completely, you can just assign a new
  238. list::
  239. request.upload_handlers = [ProgressBarUploadHandler()]
  240. .. note::
  241. You can only modify upload handlers *before* accessing
  242. ``request.POST`` or ``request.FILES`` -- it doesn't make sense to
  243. change upload handlers after upload handling has already
  244. started. If you try to modify ``request.upload_handlers`` after
  245. reading from ``request.POST`` or ``request.FILES`` Django will
  246. throw an error.
  247. Thus, you should always modify uploading handlers as early in your view as
  248. possible.
  249. Also, ``request.POST`` is accessed by
  250. :class:`~django.middleware.csrf.CsrfViewMiddleware` which is enabled by
  251. default. This means you will need to use
  252. :func:`~django.views.decorators.csrf.csrf_exempt` on your view to allow you
  253. to change the upload handlers. You will then need to use
  254. :func:`~django.views.decorators.csrf.csrf_protect` on the function that
  255. actually processes the request. Note that this means that the handlers may
  256. start receiving the file upload before the CSRF checks have been done.
  257. Example code:
  258. .. code-block:: python
  259. from django.views.decorators.csrf import csrf_exempt, csrf_protect
  260. @csrf_exempt
  261. def upload_file_view(request):
  262. request.upload_handlers.insert(0, ProgressBarUploadHandler())
  263. return _upload_file_view(request)
  264. @csrf_protect
  265. def _upload_file_view(request):
  266. ... # Process request
  267. Writing custom upload handlers
  268. ------------------------------
  269. .. currentmodule:: django.core.files.uploadhandler
  270. .. class:: FileUploadHandler
  271. All file upload handlers should be subclasses of
  272. ``django.core.files.uploadhandler.FileUploadHandler``. You can define upload
  273. handlers wherever you wish.
  274. Required methods
  275. ~~~~~~~~~~~~~~~~
  276. Custom file upload handlers **must** define the following methods:
  277. .. method:: FileUploadHandler.receive_data_chunk(self, raw_data, start)
  278. Receives a "chunk" of data from the file upload.
  279. ``raw_data`` is a byte string containing the uploaded data.
  280. ``start`` is the position in the file where this ``raw_data`` chunk
  281. begins.
  282. The data you return will get fed into the subsequent upload handlers'
  283. ``receive_data_chunk`` methods. In this way, one handler can be a
  284. "filter" for other handlers.
  285. Return ``None`` from ``receive_data_chunk`` to short-circuit remaining
  286. upload handlers from getting this chunk. This is useful if you're
  287. storing the uploaded data yourself and don't want future handlers to
  288. store a copy of the data.
  289. If you raise a ``StopUpload`` or a ``SkipFile`` exception, the upload
  290. will abort or the file will be completely skipped.
  291. .. method:: FileUploadHandler.file_complete(self, file_size)
  292. Called when a file has finished uploading.
  293. The handler should return an ``UploadedFile`` object that will be stored
  294. in ``request.FILES``. Handlers may also return ``None`` to indicate that
  295. the ``UploadedFile`` object should come from subsequent upload handlers.
  296. Optional methods
  297. ~~~~~~~~~~~~~~~~
  298. Custom upload handlers may also define any of the following optional methods or
  299. attributes:
  300. .. attribute:: FileUploadHandler.chunk_size
  301. Size, in bytes, of the "chunks" Django should store into memory and feed
  302. into the handler. That is, this attribute controls the size of chunks
  303. fed into ``FileUploadHandler.receive_data_chunk``.
  304. For maximum performance the chunk sizes should be divisible by ``4`` and
  305. should not exceed 2 GB (2\ :sup:`31` bytes) in size. When there are
  306. multiple chunk sizes provided by multiple handlers, Django will use the
  307. smallest chunk size defined by any handler.
  308. The default is 64*2\ :sup:`10` bytes, or 64 KB.
  309. .. method:: FileUploadHandler.new_file(self, field_name, file_name, content_type, content_length, charset, content_type_extra)
  310. Callback signaling that a new file upload is starting. This is called
  311. before any data has been fed to any upload handlers.
  312. ``field_name`` is a string name of the file ``<input>`` field.
  313. ``file_name`` is the unicode filename that was provided by the browser.
  314. ``content_type`` is the MIME type provided by the browser -- E.g.
  315. ``'image/jpeg'``.
  316. ``content_length`` is the length of the image given by the browser.
  317. Sometimes this won't be provided and will be ``None``.
  318. ``charset`` is the character set (i.e. ``utf8``) given by the browser.
  319. Like ``content_length``, this sometimes won't be provided.
  320. ``content_type_extra`` is extra information about the file from the
  321. ``content-type`` header. See :attr:`UploadedFile.content_type_extra
  322. <django.core.files.uploadedfile.UploadedFile.content_type_extra>`.
  323. This method may raise a ``StopFutureHandlers`` exception to prevent
  324. future handlers from handling this file.
  325. .. versionadded:: 1.7
  326. The ``content_type_extra`` parameter was added.
  327. .. method:: FileUploadHandler.upload_complete(self)
  328. Callback signaling that the entire upload (all files) has completed.
  329. .. method:: FileUploadHandler.handle_raw_input(self, input_data, META, content_length, boundary, encoding)
  330. Allows the handler to completely override the parsing of the raw
  331. HTTP input.
  332. ``input_data`` is a file-like object that supports ``read()``-ing.
  333. ``META`` is the same object as ``request.META``.
  334. ``content_length`` is the length of the data in ``input_data``. Don't
  335. read more than ``content_length`` bytes from ``input_data``.
  336. ``boundary`` is the MIME boundary for this request.
  337. ``encoding`` is the encoding of the request.
  338. Return ``None`` if you want upload handling to continue, or a tuple of
  339. ``(POST, FILES)`` if you want to return the new data structures suitable
  340. for the request directly.