file-uploads.txt 18 KB

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