uploads.txt 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. ==================================
  2. Uploaded Files and Upload Handlers
  3. ==================================
  4. .. module:: django.core.files.uploadedfile
  5. :synopsis: Classes representing uploaded files.
  6. Uploaded files
  7. ==============
  8. .. class:: UploadedFile
  9. During file uploads, the actual file data is stored in :attr:`request.FILES
  10. <django.http.HttpRequest.FILES>`. Each entry in this dictionary is an
  11. ``UploadedFile`` object (or a subclass) -- a simple wrapper around an uploaded
  12. file. You'll usually use one of these methods to access the uploaded content:
  13. .. method:: UploadedFile.read()
  14. Read the entire uploaded data from the file. Be careful with this method:
  15. if the uploaded file is huge it can overwhelm your system if you try to
  16. read it into memory. You'll probably want to use ``chunks()`` instead; see
  17. below.
  18. .. method:: UploadedFile.multiple_chunks(chunk_size=None)
  19. Returns ``True`` if the uploaded file is big enough to require reading in
  20. multiple chunks. By default this will be any file larger than 2.5 megabytes,
  21. but that's configurable; see below.
  22. .. method:: UploadedFile.chunks(chunk_size=None)
  23. A generator returning chunks of the file. If ``multiple_chunks()`` is
  24. ``True``, you should use this method in a loop instead of ``read()``.
  25. In practice, it's often easiest simply to use ``chunks()`` all the time.
  26. Looping over ``chunks()`` instead of using ``read()`` ensures that large
  27. files don't overwhelm your system's memory.
  28. Here are some useful attributes of ``UploadedFile``:
  29. .. attribute:: UploadedFile.name
  30. The name of the uploaded file (e.g. ``my_file.txt``).
  31. .. attribute:: UploadedFile.size
  32. The size, in bytes, of the uploaded file.
  33. .. attribute:: UploadedFile.content_type
  34. The content-type header uploaded with the file (e.g. :mimetype:`text/plain`
  35. or :mimetype:`application/pdf`). Like any data supplied by the user, you
  36. shouldn't trust that the uploaded file is actually this type. You'll still
  37. need to validate that the file contains the content that the content-type
  38. header claims -- "trust but verify."
  39. .. attribute:: UploadedFile.content_type_extra
  40. A dictionary containing extra parameters passed to the ``content-type``
  41. header. This is typically provided by services, such as Google App Engine,
  42. that intercept and handle file uploads on your behalf. As a result your
  43. handler may not receive the uploaded file content, but instead a URL or
  44. other pointer to the file. (see `RFC 2388`_ section 5.3).
  45. .. _RFC 2388: https://www.ietf.org/rfc/rfc2388.txt
  46. .. attribute:: UploadedFile.charset
  47. For :mimetype:`text/*` content-types, the character set (i.e. ``utf8``)
  48. supplied by the browser. Again, "trust but verify" is the best policy here.
  49. .. note::
  50. Like regular Python files, you can read the file line-by-line simply by
  51. iterating over the uploaded file:
  52. .. code-block:: python
  53. for line in uploadedfile:
  54. do_something_with(line)
  55. Lines are split using `universal newlines`_. The following are recognized
  56. as ending a line: the Unix end-of-line convention ``'\n'``, the Windows
  57. convention ``'\r\n'``, and the old Macintosh convention ``'\r'``.
  58. .. _universal newlines: https://www.python.org/dev/peps/pep-0278
  59. Subclasses of ``UploadedFile`` include:
  60. .. class:: TemporaryUploadedFile
  61. A file uploaded to a temporary location (i.e. stream-to-disk). This class
  62. is used by the
  63. :class:`~django.core.files.uploadhandler.TemporaryFileUploadHandler`. In
  64. addition to the methods from :class:`UploadedFile`, it has one additional
  65. method:
  66. .. method:: TemporaryUploadedFile.temporary_file_path()
  67. Returns the full path to the temporary uploaded file.
  68. .. class:: InMemoryUploadedFile
  69. A file uploaded into memory (i.e. stream-to-memory). This class is used
  70. by the :class:`~django.core.files.uploadhandler.MemoryFileUploadHandler`.
  71. Built-in upload handlers
  72. ========================
  73. .. module:: django.core.files.uploadhandler
  74. :synopsis: Django's handlers for file uploads.
  75. Together the :class:`MemoryFileUploadHandler` and
  76. :class:`TemporaryFileUploadHandler` provide Django's default file upload
  77. behavior of reading small files into memory and large ones onto disk. They
  78. are located in ``django.core.files.uploadhandler``.
  79. .. class:: MemoryFileUploadHandler
  80. File upload handler to stream uploads into memory (used for small files).
  81. .. class:: TemporaryFileUploadHandler
  82. Upload handler that streams data into a temporary file using
  83. :class:`~django.core.files.uploadedfile.TemporaryUploadedFile`.
  84. .. _custom_upload_handlers:
  85. Writing custom upload handlers
  86. ==============================
  87. .. class:: FileUploadHandler
  88. All file upload handlers should be subclasses of
  89. ``django.core.files.uploadhandler.FileUploadHandler``. You can define upload
  90. handlers wherever you wish.
  91. Required methods
  92. ----------------
  93. Custom file upload handlers **must** define the following methods:
  94. .. method:: FileUploadHandler.receive_data_chunk(raw_data, start)
  95. Receives a "chunk" of data from the file upload.
  96. ``raw_data`` is a bytestring containing the uploaded data.
  97. ``start`` is the position in the file where this ``raw_data`` chunk
  98. begins.
  99. The data you return will get fed into the subsequent upload handlers'
  100. ``receive_data_chunk`` methods. In this way, one handler can be a
  101. "filter" for other handlers.
  102. Return ``None`` from ``receive_data_chunk`` to short-circuit remaining
  103. upload handlers from getting this chunk. This is useful if you're
  104. storing the uploaded data yourself and don't want future handlers to
  105. store a copy of the data.
  106. If you raise a ``StopUpload`` or a ``SkipFile`` exception, the upload
  107. will abort or the file will be completely skipped.
  108. .. method:: FileUploadHandler.file_complete(file_size)
  109. Called when a file has finished uploading.
  110. The handler should return an ``UploadedFile`` object that will be stored
  111. in ``request.FILES``. Handlers may also return ``None`` to indicate that
  112. the ``UploadedFile`` object should come from subsequent upload handlers.
  113. Optional methods
  114. ----------------
  115. Custom upload handlers may also define any of the following optional methods or
  116. attributes:
  117. .. attribute:: FileUploadHandler.chunk_size
  118. Size, in bytes, of the "chunks" Django should store into memory and feed
  119. into the handler. That is, this attribute controls the size of chunks
  120. fed into ``FileUploadHandler.receive_data_chunk``.
  121. For maximum performance the chunk sizes should be divisible by ``4`` and
  122. should not exceed 2 GB (2\ :sup:`31` bytes) in size. When there are
  123. multiple chunk sizes provided by multiple handlers, Django will use the
  124. smallest chunk size defined by any handler.
  125. The default is 64*2\ :sup:`10` bytes, or 64 KB.
  126. .. method:: FileUploadHandler.new_file(field_name, file_name, content_type, content_length, charset, content_type_extra)
  127. Callback signaling that a new file upload is starting. This is called
  128. before any data has been fed to any upload handlers.
  129. ``field_name`` is a string name of the file ``<input>`` field.
  130. ``file_name`` is the filename provided by the browser.
  131. ``content_type`` is the MIME type provided by the browser -- E.g.
  132. ``'image/jpeg'``.
  133. ``content_length`` is the length of the image given by the browser.
  134. Sometimes this won't be provided and will be ``None``.
  135. ``charset`` is the character set (i.e. ``utf8``) given by the browser.
  136. Like ``content_length``, this sometimes won't be provided.
  137. ``content_type_extra`` is extra information about the file from the
  138. ``content-type`` header. See :attr:`UploadedFile.content_type_extra
  139. <django.core.files.uploadedfile.UploadedFile.content_type_extra>`.
  140. This method may raise a ``StopFutureHandlers`` exception to prevent
  141. future handlers from handling this file.
  142. .. method:: FileUploadHandler.upload_complete()
  143. Callback signaling that the entire upload (all files) has completed.
  144. .. method:: FileUploadHandler.handle_raw_input(input_data, META, content_length, boundary, encoding)
  145. Allows the handler to completely override the parsing of the raw
  146. HTTP input.
  147. ``input_data`` is a file-like object that supports ``read()``-ing.
  148. ``META`` is the same object as ``request.META``.
  149. ``content_length`` is the length of the data in ``input_data``. Don't
  150. read more than ``content_length`` bytes from ``input_data``.
  151. ``boundary`` is the MIME boundary for this request.
  152. ``encoding`` is the encoding of the request.
  153. Return ``None`` if you want upload handling to continue, or a tuple of
  154. ``(POST, FILES)`` if you want to return the new data structures suitable
  155. for the request directly.