Uploaded Files and Upload HandlersLink to this heading

Uploaded filesLink to this heading

class UploadedFileLink to this definition

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 simple wrapper around an uploaded file. You'll usually use one of these methods to access the uploaded content:

UploadedFile.read()Link to this definition

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)Link to this definition

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)Link to this definition

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 simply to use chunks() all the time. Looping over chunks() instead of using read() ensures that large files don't overwhelm your system's memory.

Here are some useful attributes of UploadedFile:

UploadedFile.nameLink to this definition

The name of the uploaded file (e.g. my_file.txt).

UploadedFile.sizeLink to this definition

The size, in bytes, of the uploaded file.

UploadedFile.content_typeLink to this definition

The content-type header uploaded with the file (e.g. text/plain or application/pdf). Like any data supplied by the user, you shouldn't trust that the uploaded file is actually this type. You'll still need to validate that the file contains the content that the content-type header claims -- "trust but verify."

UploadedFile.content_type_extraLink to this definition

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 section 5.3).

UploadedFile.charsetLink to this definition

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

Subclasses of UploadedFile include:

class TemporaryUploadedFileLink to this definition

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()Link to this definition

Returns the full path to the temporary uploaded file.

class InMemoryUploadedFileLink to this definition

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

Built-in upload handlersLink to this heading

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 MemoryFileUploadHandlerLink to this definition

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

class TemporaryFileUploadHandlerLink to this definition

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

アップロードハンドラをカスタマイズするLink to this heading

class FileUploadHandlerLink to this definition

全てのファイルアップロードハンドラは、django.core.files.uploadhandler.FileUploadHandler のサブクラスとなります。アップロードハンドラはいつでも好きなときに定義できます。

必要なメソッドLink to this heading

カスタマイズされたファイルアップロードハンドラでは、以下のメソッドを定義する 必要があります

FileUploadHandler.receive_data_chunk(raw_data, start)Link to this definition

ファイルアップロードから、データの "chunk" を受け取ります。

raw_data は、アップロードされたデータを含むバイト文字列です。

start はこの raw_data チャンクが始まる、ファイル内での位置です。

あなたが返すデータは、その後のアップロードハンドラの receive_data_chunk メソッドに渡されます。 この方法で、1 つのハンドラは他のハンドラの「フィルタ」になることができます。

receive_data_chunk から None を返し、残りのアップロードハンドラがこのチャンクを取得するのを短絡します。 これは、アップロードしたデータを自分で保存しておき、将来のハンドラでデータのコピーを保存したくない場合に便利です。

StopUpload または SkipFile 例外を発生させると、アップロードが中止されるか、ファイルが完全にスキップされます。

FileUploadHandler.file_complete(file_size)Link to this definition

ファイルのアップロードが完了したときに呼ばれます。

ハンドラは request.FILES に格納される UploadedFile オブジェクトを返します。 ハンドラは Upload`File オブジェクトが後続のアップロードハンドラから来るべきであることを示すために None を返すかもしれません。

省略可能なメソッドLink to this heading

独自のアップロードハンドラは、以下の省略可能なメソッドや属性を定義することができます:

FileUploadHandler.chunk_sizeLink to this definition

Django がメモリに格納してハンドラに送り込む "chunks" のサイズです (byte で表されます)。 つまり、この属性は FileUploadHandler.receive_data_chunk に渡されるチャンクのサイズをコントロールします。

パフォーマンスを最大限にするために、チャンクサイズは 4 で割り切れる必要があり、2 GB (231 bytes) を超えてはいけません。 複数のハンドラによってチャンクサイズが複数ある場合、Django はハンドラで定義された最小のチャンクサイズを使用します。

デフォルトは 64*210 bytes、つまり 64 KB です。

FileUploadHandler.new_file(field_name, file_name, content_type, content_length, charset, content_type_extra)Link to this definition

新しいファイルのアップロード開始を通知するコールバックです。 アップロードハンドラにデータが送られる前に呼び出されます。

field_name は、ファイル <input> フィールドの文字列名です。

file_name は、ブラウザによって提供されるユニコードのファイル名です。

content_type は、ブラウザによって提供される MIME タイプです -- 例: 'image/jpeg'

content_length は、ブラウザによって与えられる画像の長さです。提供されずに None となることがあります。

charset は、ブラウザによって与えられる文字セットです (例: utf8)。content_length のように、提供されないことがあります。

content_type_extra は、content-type からの、ファイルについての追加情報です。UploadedFile.content_type_extra をご覧ください。

このメソッドは、将来のハンドラがこのファイルをハンドリングするのを防ぐため、StopFutureHandlers 例外を投げます。

FileUploadHandler.upload_complete()Link to this definition

全てのアップロード (全てのファイル) が完了したことを通知するコールバックです。

FileUploadHandler.handle_raw_input(input_data, META, content_length, boundary, encoding)Link to this definition

ハンドラに対して、生の HTTP インプットのパースを完全にオーバーライドできるようにします。

input_data は、read() をサポートするファイルのようなオブジェクトです。

META は、request.META と同じオブジェクトです。

content_length は、input_data 内のデータの長さです。input_data` から ``content_length バイト以上を読み出さないでください。

boundary は、このリクエストの MIME

encoding は、リクエストのエンコーディングです。

アップロードハンドリングを継続したいとき、None を返します。もしくはリクエストに適した新しいデータ構造を直接返したいとき、 (POST, FILES) のタプルを返します。