Skip to main content

FileUploadHandler

Base class for streaming upload handlers.

Attributes

AttributeTypeDescription
chunk_sizeint = 65536The default chunk size is 64 KB.

Constructor

Signature

def FileUploadHandler(
request: object = None
) - > null

Parameters

NameTypeDescription
requestobject = NoneThe optional request object associated with the file upload.

Methods


handle_raw_input()

@classmethod
def handle_raw_input(
input_data: file-like object,
META: dict,
content_length: int,
boundary: str,
encoding: str = None
)

Handle the raw input from the client.

Parameters

NameTypeDescription
input_datafile-like objectAn object that supports reading via .read() containing the raw stream from the client
METAdictThe request.META dictionary containing server and environment metadata
content_lengthintThe integer value of the Content-Length header provided by the client
boundarystrThe multipart boundary string from the Content-Type header, excluding the leading dashes
encodingstr = NoneThe character encoding used for the request body

new_file()

@classmethod
def new_file(
field_name: str,
file_name: str,
content_type: str,
content_length: int,
charset: str = None,
content_type_extra: dict = None
)

Signal that a new file has been started.

Parameters

NameTypeDescription
field_namestrThe name of the HTML form field associated with the file upload
file_namestrThe original name of the file as provided by the client
content_typestrThe MIME type of the file (e.g., 'image/jpeg')
content_lengthintThe claimed size of the file; note that this value from the client may be untrustworthy
charsetstr = NoneThe character set of the file if specified in the Content-Type header
content_type_extradict = NoneAdditional parameters found in the Content-Type header

receive_data_chunk()

@classmethod
def receive_data_chunk(
raw_data: bytes,
start: int
)

Receive data from the streamed upload parser. start is the position in the file of the chunk.

Parameters

NameTypeDescription
raw_databytesThe byte string containing a chunk of data from the uploaded file
startintThe byte offset within the file where this specific chunk begins

file_complete()

@classmethod
def file_complete(
file_size: int
) - > [UploadedFile](../uploadedfile/uploadedfile.md?sid=django_core_files_uploadedfile_uploadedfile)

Signal that a file has completed. File size corresponds to the actual size accumulated by all the chunks.

Parameters

NameTypeDescription
file_sizeintThe total number of bytes actually received for the file

Returns

TypeDescription
[UploadedFile](../uploadedfile/uploadedfile.md?sid=django_core_files_uploadedfile_uploadedfile)A valid UploadedFile object representing the finished upload

upload_complete()

@classmethod
def upload_complete()

Signal that the upload is complete. Subclasses should perform cleanup that is necessary for this handler.


upload_interrupted()

@classmethod
def upload_interrupted()

Signal that the upload was interrupted. Subclasses should perform cleanup that is necessary for this handler.