Movatterモバイル変換


[0]ホーム

URL:


Skip to content
Join theFastAPI Cloud waiting list 🚀
Follow@fastapi onX (Twitter) to stay updated
FollowFastAPI onLinkedIn to stay updated
Subscribe to theFastAPI and friends newsletter 🎉
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor

UploadFile class

You can definepath operation function parameters to be of the typeUploadFile to receive files from the request.

You can import it directly fromfastapi:

fromfastapiimportUploadFile

fastapi.UploadFile

UploadFile(file,*,size=None,filename=None,headers=None)

Bases:UploadFile

A file uploaded in a request.

Define it as apath operation function (or dependency) parameter.

If you are using a regulardef function, you can use theupload_file.fileattribute to access the raw standard Python file (blocking, not async), useful andneeded for non-async code.

Read more about it in theFastAPI docs for Request Files.

Example

fromtypingimportAnnotatedfromfastapiimportFastAPI,File,UploadFileapp=FastAPI()@app.post("/files/")asyncdefcreate_file(file:Annotated[bytes,File()]):return{"file_size":len(file)}@app.post("/uploadfile/")asyncdefcreate_upload_file(file:UploadFile):return{"filename":file.filename}
Source code instarlette/datastructures.py
417418419420421422423424425426427428429430431432
def__init__(self,file:BinaryIO,*,size:int|None=None,filename:str|None=None,headers:Headers|None=None,)->None:self.filename=filenameself.file=fileself.size=sizeself.headers=headersorHeaders()# Capture max size from SpooledTemporaryFile if one is provided. This slightly speeds up future checks.# Note 0 means unlimited mirroring SpooledTemporaryFile's __init__self._max_mem_size=getattr(self.file,"_max_size",0)

fileinstance-attribute

file

The standard Python file object (non-async).

filenameinstance-attribute

filename

The original file name.

sizeinstance-attribute

size

The size of the file in bytes.

headersinstance-attribute

headers

The headers of the request.

content_typeinstance-attribute

content_type

The content type of the request, from the headers.

readasync

read(size=-1)

Read some bytes from the file.

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

PARAMETERDESCRIPTION
size

The number of bytes to read from the file.

TYPE:intDEFAULT:-1

Source code infastapi/datastructures.py
 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102
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)

writeasync

write(data)

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.

PARAMETERDESCRIPTION
data

The bytes to write to the file.

TYPE:bytes

Source code infastapi/datastructures.py
66676869707172737475767778798081828384
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)

seekasync

seek(offset)

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.

PARAMETERDESCRIPTION
offset

The position in bytes to seek to in the file.

TYPE:int

Source code infastapi/datastructures.py
104105106107108109110111112113114115116117118119120121122
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)

closeasync

close()

Close the file.

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

Source code infastapi/datastructures.py
124125126127128129130
asyncdefclose(self)->None:"""    Close the file.    To be awaitable, compatible with async, this is run in threadpool.    """returnawaitsuper().close()

[8]ページ先頭

©2009-2026 Movatter.jp