# Dokumentasi Django 4.2.9.dev20231204082947

# Berkas Terunggah dan Penangan Unggah

## Berkas-berkas terunggah

class `UploadedFile`

During file uploads, the actual file data is stored in `request.FILES`. Each entry in this dictionary is an `UploadedFile` object (or a subclass) -- a wrapper around an uploaded file. You'll usually use one of these methods to access the uploaded content:

`UploadedFile.``read`()

  Read the entire uploaded data from the file. Be careful with this method: if the uploaded file is huge it can overwhelm your system if you try to read it into memory. You'll probably want to use `chunks()` instead; see below.

`UploadedFile.``multiple_chunks`(chunk_size=None)

  Returns `True` if the uploaded file is big enough to require reading in multiple chunks. By default this will be any file larger than 2.5 megabytes, but that's configurable; see below.

`UploadedFile.``chunks`(chunk_size=None)

  A generator returning chunks of the file. If `multiple_chunks()` is `True`, you should use this method in a loop instead of `read()`.

  In practice, it's often easiest to use `chunks()` all the time. Looping over `chunks()` instead of using `read()` ensures that large files don't overwhelm your system's memory.

Ini adalah atribut-atribut berguna dari `UploadedFile`:

`UploadedFile.``name`

  Nama dari berkas terunggah (misalnya `my_file.txt`).

`UploadedFile.``size`

  Ukuran, dalam byte, dari berkas terunggah.

`UploadedFile.``content_type`

  Kepala jenis-isi dinunggah dengan berkas (misalnya text/plain atau application/pdf). Seperti data apapun disokong oleh pengguna, anda tidak harus percaya bahwa berkas terunggah sebenarnya jenis ini. Anda akan masih butuh mensahkan bahwa berkas mengandung isi yang kepala jenis-isi tagihkan -- "trust but verify."

`UploadedFile.``content_type_extra`

  A dictionary containing extra parameters passed to the `content-type` header. This is typically provided by services, such as Google App Engine, that intercept and handle file uploads on your behalf. As a result your handler may not receive the uploaded file content, but instead a URL or other pointer to the file (see RFC 2388).

`UploadedFile.``charset`

  For text/* content-types, the character set (i.e. `utf8`) supplied by the browser. Again, "trust but verify" is the best policy here.

Catatan

Like regular Python files, you can read the file line-by-line by iterating over the uploaded file:

    for line in uploadedfile:
        do_something_with(line)

Lines are split using universal newlines. The following are recognized as ending a line: the Unix end-of-line convention `'\n'`, the Windows convention `'\r\n'`, and the old Macintosh convention `'\r'`.

Subkelas-subkelas dari `UploadedFile` termasuk:

class `TemporaryUploadedFile`

  A file uploaded to a temporary location (i.e. stream-to-disk). This class is used by the `TemporaryFileUploadHandler`. In addition to the methods from `UploadedFile`, it has one additional method:

`TemporaryUploadedFile.``temporary_file_path`()

  Mengembalikan jalur penuh pada berkas terunggah sementara.

class `InMemoryUploadedFile`

  A file uploaded into memory (i.e. stream-to-memory). This class is used by the `MemoryFileUploadHandler`.

## Penangan unggah siap-pakai

Together the `MemoryFileUploadHandler` and `TemporaryFileUploadHandler` provide Django's default file upload behavior of reading small files into memory and large ones onto disk. They are located in `django.core.files.uploadhandler`.

class `MemoryFileUploadHandler`

File upload handler to stream uploads into memory (used for small files).

class `TemporaryFileUploadHandler`

Upload handler that streams data into a temporary file using `TemporaryUploadedFile`.

## Menulis penangan unggah penyesuaian

class `FileUploadHandler`

All file upload handlers should be subclasses of `django.core.files.uploadhandler.FileUploadHandler`. You can define upload handlers wherever you wish.

### Metode-metode diwajibkan

Penyesuaian penangan unggah berkas harus ditentukan metode-metode berikut:

`FileUploadHandler.``receive_data_chunk`(raw_data, start)

  Menerima "chunk" data dari unggah berkas.

  `raw_data` is a bytestring containing the uploaded data.

  `start` adalah posisi dimana berkas potongan `raw_data` ini dimulai.

  The data you return will get fed into the subsequent upload handlers' `receive_data_chunk` methods. In this way, one handler can be a "filter" for other handlers.

  Return `None` from `receive_data_chunk` to short-circuit remaining upload handlers from getting this chunk. This is useful if you're storing the uploaded data yourself and don't want future handlers to store a copy of the data.

  If you raise a `StopUpload` or a `SkipFile` exception, the upload will abort or the file will be completely skipped.

`FileUploadHandler.``file_complete`(file_size)

  Dipanggil setelah sebuah berkas selesai diunggah.

  The handler should return an `UploadedFile` object that will be stored in `request.FILES`. Handlers may also return `None` to indicate that the `UploadedFile` object should come from subsequent upload handlers.

### Metode-metode pilihan

Custom upload handlers may also define any of the following optional methods or attributes:

`FileUploadHandler.``chunk_size`

  Size, in bytes, of the "chunks" Django should store into memory and feed into the handler. That is, this attribute controls the size of chunks fed into `FileUploadHandler.receive_data_chunk`.

  For maximum performance the chunk sizes should be divisible by `4` and should not exceed 2 GB (231 bytes) in size. When there are multiple chunk sizes provided by multiple handlers, Django will use the smallest chunk size defined by any handler.

  Awalan 64*210 byte, atau 64 KB.

`FileUploadHandler.``new_file`(field_name, file_name, content_type, content_length, charset, content_type_extra)

  Callback signaling that a new file upload is starting. This is called before any data has been fed to any upload handlers.

  `field_name` adalah nama string dari bidang `<input>` berkas.

  `file_name` adalah nama berkas disediakan oleh peramban.

  `content_type` adalah jenis MIME disediakan oleh peramban --Sebagai contoh `'image/jpeg'`.

  `content_length` is the length of the image given by the browser. Sometimes this won't be provided and will be `None`.

  `charset` is the character set (i.e. `utf8`) given by the browser. Like `content_length`, this sometimes won't be provided.

  `content_type_extra` is extra information about the file from the `content-type` header. See `UploadedFile.content_type_extra`.

  This method may raise a `StopFutureHandlers` exception to prevent future handlers from handling this file.

`FileUploadHandler.``upload_complete`()

  Callback signaling that the entire upload (all files) has completed.

`FileUploadHandler.``upload_interrupted`()

  Callback signaling that the upload was interrupted, e.g. when the user closed their browser during file upload.

`FileUploadHandler.``handle_raw_input`(input_data, META, content_length, boundary, encoding)

  Allows the handler to completely override the parsing of the raw HTTP input.

  `input_data` is a file-like object that supports `read()`-ing.

  `META` adalah obyek sama seperti `request.META`.

  `content_length` adalah panjang dari data dalam `input_data`. Jangan membaca lebih dari `content_length` byte dari `input_data`.

  `boundary` adalah batasan MIME untuk permintaan ini.

  `encoding` adalah penyandian dari permintaan.

  Return `None` if you want upload handling to continue, or a tuple of `(POST, FILES)` if you want to return the new data structures suitable for the request directly.