2
0

uploads.txt 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 wrapper around an uploaded file.
  12. 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 to use ``chunks()`` all the time. Looping
  26. over ``chunks()`` instead of using ``read()`` ensures that large files
  27. 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:`RFC 2388 <2388#section-5.3>`).
  45. .. attribute:: UploadedFile.charset
  46. For :mimetype:`text/*` content-types, the character set (i.e. ``utf8``)
  47. supplied by the browser. Again, "trust but verify" is the best policy here.
  48. .. note::
  49. Like regular Python files, you can read the file line-by-line by iterating
  50. over the uploaded file::
  51. for line in uploadedfile:
  52. do_something_with(line)
  53. Lines are split using :pep:`universal newlines <278>`. The following are
  54. recognized as ending a line: the Unix end-of-line convention ``'\n'``, the
  55. Windows convention ``'\r\n'``, and the old Macintosh convention ``'\r'``.
  56. Subclasses of ``UploadedFile`` include:
  57. .. class:: TemporaryUploadedFile
  58. A file uploaded to a temporary location (i.e. stream-to-disk). This class
  59. is used by the
  60. :class:`~django.core.files.uploadhandler.TemporaryFileUploadHandler`. In
  61. addition to the methods from :class:`UploadedFile`, it has one additional
  62. method:
  63. .. method:: TemporaryUploadedFile.temporary_file_path()
  64. Returns the full path to the temporary uploaded file.
  65. .. class:: InMemoryUploadedFile
  66. A file uploaded into memory (i.e. stream-to-memory). This class is used
  67. by the :class:`~django.core.files.uploadhandler.MemoryFileUploadHandler`.
  68. Built-in upload handlers
  69. ========================
  70. .. module:: django.core.files.uploadhandler
  71. :synopsis: Django's handlers for file uploads.
  72. Together the :class:`MemoryFileUploadHandler` and
  73. :class:`TemporaryFileUploadHandler` provide Django's default file upload
  74. behavior of reading small files into memory and large ones onto disk. They
  75. are located in ``django.core.files.uploadhandler``.
  76. .. class:: MemoryFileUploadHandler
  77. File upload handler to stream uploads into memory (used for small files).
  78. .. class:: TemporaryFileUploadHandler
  79. Upload handler that streams data into a temporary file using
  80. :class:`~django.core.files.uploadedfile.TemporaryUploadedFile`.
  81. .. _custom_upload_handlers:
  82. Writing custom upload handlers
  83. ==============================
  84. .. class:: FileUploadHandler
  85. All file upload handlers should be subclasses of
  86. ``django.core.files.uploadhandler.FileUploadHandler``. You can define upload
  87. handlers wherever you wish.
  88. Required methods
  89. ----------------
  90. Custom file upload handlers **must** define the following methods:
  91. .. method:: FileUploadHandler.receive_data_chunk(raw_data, start)
  92. Receives a "chunk" of data from the file upload.
  93. ``raw_data`` is a bytestring containing the uploaded data.
  94. ``start`` is the position in the file where this ``raw_data`` chunk
  95. begins.
  96. The data you return will get fed into the subsequent upload handlers'
  97. ``receive_data_chunk`` methods. In this way, one handler can be a
  98. "filter" for other handlers.
  99. Return ``None`` from ``receive_data_chunk`` to short-circuit remaining
  100. upload handlers from getting this chunk. This is useful if you're
  101. storing the uploaded data yourself and don't want future handlers to
  102. store a copy of the data.
  103. If you raise a ``StopUpload`` or a ``SkipFile`` exception, the upload
  104. will abort or the file will be completely skipped.
  105. .. method:: FileUploadHandler.file_complete(file_size)
  106. Called when a file has finished uploading.
  107. The handler should return an ``UploadedFile`` object that will be stored
  108. in ``request.FILES``. Handlers may also return ``None`` to indicate that
  109. the ``UploadedFile`` object should come from subsequent upload handlers.
  110. Optional methods
  111. ----------------
  112. Custom upload handlers may also define any of the following optional methods or
  113. attributes:
  114. .. attribute:: FileUploadHandler.chunk_size
  115. Size, in bytes, of the "chunks" Django should store into memory and feed
  116. into the handler. That is, this attribute controls the size of chunks
  117. fed into ``FileUploadHandler.receive_data_chunk``.
  118. For maximum performance the chunk sizes should be divisible by ``4`` and
  119. should not exceed 2 GB (2\ :sup:`31` bytes) in size. When there are
  120. multiple chunk sizes provided by multiple handlers, Django will use the
  121. smallest chunk size defined by any handler.
  122. The default is 64*2\ :sup:`10` bytes, or 64 KB.
  123. .. method:: FileUploadHandler.new_file(field_name, file_name, content_type, content_length, charset, content_type_extra)
  124. Callback signaling that a new file upload is starting. This is called
  125. before any data has been fed to any upload handlers.
  126. ``field_name`` is a string name of the file ``<input>`` field.
  127. ``file_name`` is the filename provided by the browser.
  128. ``content_type`` is the MIME type provided by the browser -- E.g.
  129. ``'image/jpeg'``.
  130. ``content_length`` is the length of the image given by the browser.
  131. Sometimes this won't be provided and will be ``None``.
  132. ``charset`` is the character set (i.e. ``utf8``) given by the browser.
  133. Like ``content_length``, this sometimes won't be provided.
  134. ``content_type_extra`` is extra information about the file from the
  135. ``content-type`` header. See :attr:`UploadedFile.content_type_extra
  136. <django.core.files.uploadedfile.UploadedFile.content_type_extra>`.
  137. This method may raise a ``StopFutureHandlers`` exception to prevent
  138. future handlers from handling this file.
  139. .. method:: FileUploadHandler.upload_complete()
  140. Callback signaling that the entire upload (all files) has completed.
  141. .. method:: FileUploadHandler.upload_interrupted()
  142. Callback signaling that the upload was interrupted, e.g. when the user
  143. closed their browser during file upload.
  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.