← All notes

File and UploadFile declare uploaded files sent as form data

Answer: Declare uploaded files with File or UploadFile. They arrive as form data, not JSON. Install python-multipart first. If the parameter type is bytes, FastAPI reads the file and you receive the contents as bytes. The whole contents will be stored in memory, which will work well for small files. UploadFile will work well for large files without consuming all the memory.

UploadFile

UploadFile uses a spooled file. Contents stay in memory up to a size limit, then the file is stored on disk. It has filename, content_type, and a file object that is a SpooledTemporaryFile. The async methods read, write, seek, and close must be awaited. In a plain def path operation you can call methods on .file directly.

FastAPI's UploadFile inherits from Starlette's UploadFile. Forms that include files are encoded as multipart/form-data. You can declare several File and Form fields, but you cannot also expect a JSON Body on the same request.

Starlette form uploads

In Starlette, request files also arrive as multipart form data. request.form() yields a FormData multidict that holds both text fields and uploads. Each upload is a UploadFile with filename, content_type, and a spooled file object. Await read() to get the contents.

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: fastapi/fastapi (GitHub), Request Files, MIT (https://raw.githubusercontent.com/fastapi/fastapi/master/LICENSE). Copyright (c) 2018 Sebastián Ramírez; Kludex/starlette (GitHub), Request Files (Starlette), BSD-3-Clause (https://raw.githubusercontent.com/Kludex/starlette/main/LICENSE.md). Copyright (c) 2018, Encode OSS Ltd. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: fastapi/fastapi (GitHub), Request Files, MIT (https://raw.githubusercontent.com/fastapi/fastapi/master/LICENSE). Copyright (c) 2018 Sebastián Ramírez; Kludex/starlette (GitHub), Request Files (Starlette), BSD-3-Clause (https://raw.githubusercontent.com/Kludex/starlette/main/LICENSE.md). Copyright (c) 2018, Encode OSS Ltd.

Cite this

Permanent ID: iil:note:abxxe7qx

iinventedlight. 2026. "File and UploadFile declare uploaded files sent as form data." iinventedlight Library, iil:note:abxxe7qx, version of 2026-11-17. https://iinventedlight.com/library/notes/fastapi-upload-files/. License: CC-BY-4.0.

@misc{iil_note_abxxe7qx,
  author = {{iinventedlight}},
  title = {{File and UploadFile declare uploaded files sent as form data}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-17},
  url = {https://iinventedlight.com/library/notes/fastapi-upload-files/},
  note = {Permanent ID iil:note:abxxe7qx. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/fastapi-upload-files.md.