Uploading multiple files lets users send many files at once. This saves time and makes apps easier to use.
Multiple file uploads in FastAPI
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
FastAPI
from fastapi import FastAPI, File, UploadFile from typing import List app = FastAPI() @app.post("/uploadfiles/") async def upload_files(files: List[UploadFile] = File(...)): return {"filenames": [file.filename for file in files]}
Use List[UploadFile] to accept multiple files.
The File(...) tells FastAPI this is a file upload field.
Examples
FastAPI
from fastapi import FastAPI, File, UploadFile from typing import List app = FastAPI() @app.post("/uploadfiles/") async def upload_files(files: List[UploadFile] = File(...)): return {"filenames": [file.filename for file in files]}
FastAPI
from fastapi import FastAPI, File, UploadFile from typing import List app = FastAPI() @app.post("/uploadfiles/") async def upload_files(files: List[UploadFile] = File(...)): contents = [] for file in files: content = await file.read() contents.append({"filename": file.filename, "size": len(content)}) return contents
Sample Program
This FastAPI app defines a POST endpoint /uploadfiles/ that accepts multiple files. It returns a JSON with the list of uploaded file names.
FastAPI
from fastapi import FastAPI, File, UploadFile from typing import List app = FastAPI() @app.post("/uploadfiles/") async def upload_files(files: List[UploadFile] = File(...)): filenames = [file.filename for file in files] return {"uploaded_files": filenames}
Important Notes
Remember to use async and await when reading file contents.
Use List[UploadFile] to handle multiple files cleanly.
FastAPI automatically handles the multipart form data for file uploads.
Summary
Use List[UploadFile] with File(...) to accept multiple files.
Return file info like names or sizes after upload.
FastAPI makes handling multiple file uploads simple and efficient.
Practice
1. In FastAPI, which type hint should you use to accept multiple uploaded files in an endpoint?
easy
Solution
Step 1: Understand FastAPI file upload types
FastAPI uses UploadFile to handle file uploads efficiently.Step 2: Use List for multiple files
To accept multiple files, you wrap UploadFile in a List and use File(...) to declare it as a file input.Final Answer:
List[UploadFile] with File(...) -> Option BQuick Check:
Multiple files = List[UploadFile] [OK]
Hint: Use List[UploadFile] with File(...) for multiple files [OK]
Common Mistakes:
- Using UploadFile without List for multiple files
- Using List[str] which expects strings, not files
- Confusing str type with file uploads
2. Which of the following is the correct syntax to declare a FastAPI endpoint that accepts multiple files named 'files'?
easy
Solution
Step 1: Check parameter type for multiple files
Multiple files require List[UploadFile] type hint.Step 2: Use File(...) to mark as file input
File(...) is needed to tell FastAPI this is a file upload field.Final Answer:
files: List[UploadFile] = File(...) -> Option AQuick Check:
Multiple files syntax = List[UploadFile] = File(...) [OK]
Hint: Use List[UploadFile] = File(...) for multiple files param [OK]
Common Mistakes:
- Using UploadFile without List for multiple files
- Using List[str] which is incorrect for files
- Assigning UploadFile(...) instead of File(...)
3. Given this FastAPI endpoint code, what will be the output if two files named 'a.txt' and 'b.txt' are uploaded?
from fastapi import FastAPI, File, UploadFile
from typing import List
app = FastAPI()
@app.post('/upload')
async def upload(files: List[UploadFile] = File(...)):
return {"filenames": [file.filename for file in files]}medium
Solution
Step 1: Understand the endpoint logic
The endpoint returns a dictionary with a list of filenames extracted from the uploaded files.Step 2: Check the uploaded files names
Two files named 'a.txt' and 'b.txt' are uploaded, so their filenames will be returned in the list.Final Answer:
{"filenames": ["a.txt", "b.txt"]} -> Option AQuick Check:
Returned filenames list matches uploaded files [OK]
Hint: Returned filenames list matches uploaded files names [OK]
Common Mistakes:
- Expecting file content instead of filenames
- Confusing parameter name with file names
- Assuming empty list if files uploaded
4. What is wrong with this FastAPI endpoint for multiple file uploads?
from fastapi import FastAPI, File, UploadFile
from typing import List
app = FastAPI()
@app.post('/upload')
async def upload(files: UploadFile = File(...)):
return {"count": len(files)}medium
Solution
Step 1: Check parameter type for multiple files
The parameter 'files' is typed as UploadFile, which accepts only one file.Step 2: Correct type for multiple files
To accept multiple files, it must be List[UploadFile].Final Answer:
files should be List[UploadFile] to accept multiple files -> Option DQuick Check:
Multiple files need List[UploadFile] type [OK]
Hint: Use List[UploadFile] for multiple files, not UploadFile alone [OK]
Common Mistakes:
- Using UploadFile instead of List[UploadFile] for multiple files
- Confusing File(...) with UploadFile(...)
- Forgetting async keyword (though present here)
5. You want to create a FastAPI endpoint that accepts multiple files and returns a JSON with each file's name and size in bytes. Which code snippet correctly implements this?
hard
Solution
Step 1: Accept multiple files with correct type
Use List[UploadFile] with File(...) to accept multiple files.Step 2: Read each file content to get size
Use await file.read() to get bytes, then len() to get size.Step 3: Return list of dicts with name and size
Build a list of dictionaries with filename and size for each file.Final Answer:
Code snippet that correctly reads files and returns name and size -> Option CQuick Check:
Read files async, return name and size list [OK]
Hint: Read files async, use len(content) for size, return list [OK]
Common Mistakes:
- Using UploadFile without List for multiple files
- Trying to get size without reading file content
- Using wrong attribute names like file.name or file.size
