file-uploads.txt 17 KB

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