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-asy
| 19 | |
| 20 | |
| 21 | class UploadFile(StarletteUploadFile): |
| 22 | """ |
| 23 | A file uploaded in a request. |
| 24 | |
| 25 | Define it as a *path operation function* (or dependency) parameter. |
| 26 | |
| 27 | If you are using a regular `def` function, you can use the `upload_file.file` |
| 28 | attribute to access the raw standard Python file (blocking, not async), useful and |
| 29 | needed for non-async code. |
| 30 | |
| 31 | Read more about it in the |
| 32 | [FastAPI docs for Request Files](https://fastapi.tiangolo.com/tutorial/request-files/). |
| 33 | |
| 34 | ## Example |
| 35 | |
| 36 | ```python |
| 37 | from typing import Annotated |
| 38 | |
| 39 | from fastapi import FastAPI, File, UploadFile |
| 40 | |
| 41 | app = FastAPI() |
| 42 | |
| 43 | |
| 44 | @app.post("/files/") |
| 45 | async def create_file(file: Annotated[bytes, File()]): |
| 46 | return {"file_size": len(file)} |
| 47 | |
| 48 | |
| 49 | @app.post("/uploadfile/") |
| 50 | async def create_upload_file(file: UploadFile): |
| 51 | return {"filename": file.filename} |
| 52 | ``` |
| 53 | """ |
| 54 | |
| 55 | file: Annotated[ |
| 56 | BinaryIO, |
| 57 | Doc("The standard Python file object (non-async)."), |
| 58 | ] |
| 59 | filename: Annotated[str | None, Doc("The original file name.")] |
| 60 | size: Annotated[int | None, Doc("The size of the file in bytes.")] |
| 61 | headers: Annotated[Headers, Doc("The headers of the request.")] |
| 62 | content_type: Annotated[ |
| 63 | str | None, Doc("The content type of the request, from the headers.") |
| 64 | ] |
| 65 | |
| 66 | async def write( |
| 67 | self, |
| 68 | data: Annotated[ |
| 69 | bytes, |
| 70 | Doc( |
| 71 | """ |
| 72 | The bytes to write to the file. |
| 73 | """ |
| 74 | ), |
| 75 | ], |
| 76 | ) -> None: |
| 77 | """ |
| 78 | Write some bytes to the file. |
no outgoing calls
searching dependent graphs…