Skip to main content

UploadFile

A file uploaded in a request.

Define it as a path operation function (or dependency) parameter.

If you are using a regular def function, you can use the upload_file.file attribute to access the raw standard Python file (blocking, not async), useful and needed for non-async code.

Attributes

AttributeTypeDescription
fileBinaryIOThe standard Python file object (non-async).
filename`strNone`
size`intNone`
headersHeadersThe headers of the request.
content_type`strNone`

Methods


write()

@classmethod
def write(
data: bytes
) - > None

Write some bytes to the file. You normally wouldn't use this from a file you read in a request. To be awaitable, compatible with async, this is run in threadpool.

Parameters

NameTypeDescription
databytesThe bytes to write to the file.

Returns

TypeDescription
None

read()

@classmethod
def read(
size: int = -1
) - > bytes

Read some bytes from the file. To be awaitable, compatible with async, this is run in threadpool.

Parameters

NameTypeDescription
sizeint = -1The number of bytes to read from the file.

Returns

TypeDescription
bytes

seek()

@classmethod
def seek(
offset: int
) - > None

Move to a position in the file. Any next read or write will be done from that position. To be awaitable, compatible with async, this is run in threadpool.

Parameters

NameTypeDescription
offsetintThe position in bytes to seek to in the file.

Returns

TypeDescription
None

close()

@classmethod
def close() - > None

Close the file. To be awaitable, compatible with async, this is run in threadpool.

Returns

TypeDescription
None