UploadFile class

You can define path operation function parameters to be of the type UploadFile to receive files from the request.

You can import it directly from fastapi:

  1. from fastapi import UploadFile

fastapi.UploadFile

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

Bases: 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.

Read more about it in the FastAPI docs for Request Files.

Example

  1. from typing import Annotated
  2. from fastapi import FastAPI, File, UploadFile
  3. app = FastAPI()
  4. @app.post("/files/")
  5. async def create_file(file: Annotated[bytes, File()]):
  6. return {"file_size": len(file)}
  7. @app.post("/uploadfile/")
  8. async def create_upload_file(file: UploadFile):
  9. return {"filename": file.filename}
PARAMETERDESCRIPTION
file

TYPE: BinaryIO

size

TYPE: Optional[int] DEFAULT: None

filename

TYPE: Optional[str] DEFAULT: None

headers

TYPE: Optional[Headers] DEFAULT: None

Source code in starlette/datastructures.py

  1. 437
  2. 438
  3. 439
  4. 440
  5. 441
  6. 442
  7. 443
  8. 444
  9. 445
  10. 446
  11. 447
  12. 448
  1. def init(
  2. self,
  3. file: typing.BinaryIO,
  4. *,
  5. size: typing.Optional[int] = None,
  6. filename: typing.Optional[str] = None,
  7. headers: typing.Optional[Headers]” = None,
  8. ) -> None:
  9. self.filename = filename
  10. self.file = file
  11. self.size = size
  12. self.headers = headers or Headers()

file instance-attribute

  1. file

The standard Python file object (non-async).

filename instance-attribute

  1. filename

The original file name.

size instance-attribute

  1. size

The size of the file in bytes.

headers instance-attribute

  1. headers

The headers of the request.

content_type instance-attribute

  1. content_type

The content type of the request, from the headers.

read async

  1. 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: int DEFAULT: -1

Source code in fastapi/datastructures.py

  1. 95
  2. 96
  3. 97
  4. 98
  5. 99
  6. 100
  7. 101
  8. 102
  9. 103
  10. 104
  11. 105
  12. 106
  13. 107
  14. 108
  15. 109
  16. 110
  17. 111
  1. async def read(
  2. self,
  3. size: Annotated[
  4. int,
  5. Doc(
  6. “””
  7. The number of bytes to read from the file.
  8. “””
  9. ),
  10. ] = -1,
  11. ) -> bytes:
  12. “””
  13. Read some bytes from the file.
  14. To be awaitable, compatible with async, this is run in threadpool.
  15. “””
  16. return await super().read(size)

write async

  1. 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 in fastapi/datastructures.py

  1. 75
  2. 76
  3. 77
  4. 78
  5. 79
  6. 80
  7. 81
  8. 82
  9. 83
  10. 84
  11. 85
  12. 86
  13. 87
  14. 88
  15. 89
  16. 90
  17. 91
  18. 92
  19. 93
  1. async def write(
  2. self,
  3. data: Annotated[
  4. bytes,
  5. Doc(
  6. “””
  7. The bytes to write to the file.
  8. “””
  9. ),
  10. ],
  11. ) -> None:
  12. “””
  13. Write some bytes to the file.
  14. You normally wouldnt use this from a file you read in a request.
  15. To be awaitable, compatible with async, this is run in threadpool.
  16. “””
  17. return await super().write(data)

seek async

  1. 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 in fastapi/datastructures.py

  1. 113
  2. 114
  3. 115
  4. 116
  5. 117
  6. 118
  7. 119
  8. 120
  9. 121
  10. 122
  11. 123
  12. 124
  13. 125
  14. 126
  15. 127
  16. 128
  17. 129
  18. 130
  19. 131
  1. async def seek(
  2. self,
  3. offset: Annotated[
  4. int,
  5. Doc(
  6. “””
  7. The position in bytes to seek to in the file.
  8. “””
  9. ),
  10. ],
  11. ) -> None:
  12. “””
  13. Move to a position in the file.
  14. Any next read or write will be done from that position.
  15. To be awaitable, compatible with async, this is run in threadpool.
  16. “””
  17. return await super().seek(offset)

close async

  1. close()

Close the file.

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

Source code in fastapi/datastructures.py

  1. 133
  2. 134
  3. 135
  4. 136
  5. 137
  6. 138
  7. 139
  1. async def close(self) -> None:
  2. “””
  3. Close the file.
  4. To be awaitable, compatible with async, this is run in threadpool.
  5. “””
  6. return await super().close()