UploadFile Class — fastapi Architecture
Architecture documentation for the UploadFile class in datastructures.py from the fastapi codebase.
Entity Profile
Dependency Diagram
graph TD e7f7f9d0_3e61_b391_e95f_2212c3046b08["UploadFile"] e7f7f9d0_3e61_b391_e95f_2212c3046b08["UploadFile"] e7f7f9d0_3e61_b391_e95f_2212c3046b08 -->|extends| e7f7f9d0_3e61_b391_e95f_2212c3046b08 dc4a1804_f7b4_848b_3280_30523680d7b9["datastructures.py"] e7f7f9d0_3e61_b391_e95f_2212c3046b08 -->|defined in| dc4a1804_f7b4_848b_3280_30523680d7b9 bd22fc1b_4f68_64ff_378d_a081f71b63c3["write()"] e7f7f9d0_3e61_b391_e95f_2212c3046b08 -->|method| bd22fc1b_4f68_64ff_378d_a081f71b63c3 1d8546f2_90ed_9120_dbc5_d713e7cf8085["read()"] e7f7f9d0_3e61_b391_e95f_2212c3046b08 -->|method| 1d8546f2_90ed_9120_dbc5_d713e7cf8085 c591c04f_f53b_0c31_4022_096ff9bf737b["seek()"] e7f7f9d0_3e61_b391_e95f_2212c3046b08 -->|method| c591c04f_f53b_0c31_4022_096ff9bf737b 76af83fd_46f7_5935_efc3_87a0773b471d["close()"] e7f7f9d0_3e61_b391_e95f_2212c3046b08 -->|method| 76af83fd_46f7_5935_efc3_87a0773b471d 4a2d48d6_8997_af53_93c8_8a157af0c15a["_validate()"] e7f7f9d0_3e61_b391_e95f_2212c3046b08 -->|method| 4a2d48d6_8997_af53_93c8_8a157af0c15a 9349e819_1a2c_f5b3_a5de_bac4bf977f83["__get_pydantic_json_schema__()"] e7f7f9d0_3e61_b391_e95f_2212c3046b08 -->|method| 9349e819_1a2c_f5b3_a5de_bac4bf977f83 9be5e1e8_1a6a_82d4_62d1_cb95697c7fef["__get_pydantic_core_schema__()"] e7f7f9d0_3e61_b391_e95f_2212c3046b08 -->|method| 9be5e1e8_1a6a_82d4_62d1_cb95697c7fef
Relationship Graph
Source Code
fastapi/datastructures.py lines 23–152
class UploadFile(StarletteUploadFile):
"""
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.
Read more about it in the
[FastAPI docs for Request Files](https://fastapi.tiangolo.com/tutorial/request-files/).
## Example
```python
from typing import Annotated
from fastapi import FastAPI, File, UploadFile
app = FastAPI()
@app.post("/files/")
async def create_file(file: Annotated[bytes, File()]):
return {"file_size": len(file)}
@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile):
return {"filename": file.filename}
```
"""
file: Annotated[
BinaryIO,
Doc("The standard Python file object (non-async)."),
]
filename: Annotated[Optional[str], Doc("The original file name.")]
size: Annotated[Optional[int], Doc("The size of the file in bytes.")]
headers: Annotated[Headers, Doc("The headers of the request.")]
content_type: Annotated[
Optional[str], Doc("The content type of the request, from the headers.")
]
async def write(
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.
"""
return await super().write(data)
async def read(
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.
"""
Domain
Defined In
Extends
Source
Frequently Asked Questions
What is the UploadFile class?
UploadFile is a class in the fastapi codebase, defined in fastapi/datastructures.py.
Where is UploadFile defined?
UploadFile is defined in fastapi/datastructures.py at line 23.
What does UploadFile extend?
UploadFile extends UploadFile.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free