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.
asyncdefread(self,size:Annotated[int,Doc(""" The number of bytes to read from the file. """),]=-1,)->bytes:""" Read some bytes from the file. To be awaitable, compatible with async, this is run in threadpool. """returnawaitsuper().read(size)
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.
PARAMETER
DESCRIPTION
data
The bytes to write to the file.
TYPE:bytes
Source code in fastapi/datastructures.py
75767778798081828384858687888990919293
asyncdefwrite(self,data:Annotated[bytes,Doc(""" The bytes to write to the file. """),],)->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. """returnawaitsuper().write(data)
asyncdefseek(self,offset:Annotated[int,Doc(""" The position in bytes to seek to in the file. """),],)->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. """returnawaitsuper().seek(offset)