Consider this FastAPI endpoint that accepts multiple files:
from fastapi import FastAPI, UploadFile, File
from typing import List
app = FastAPI()
@app.post('/upload')
async def upload(files: List[UploadFile] = File(...)):
return {'filenames': [file.filename for file in files]}What will be the output if a client uploads three files named a.txt, b.txt, and c.txt?
from fastapi import FastAPI, UploadFile, File from typing import List app = FastAPI() @app.post('/upload') async def upload(files: List[UploadFile] = File(...)): return {'filenames': [file.filename for file in files]}
Think about how FastAPI collects multiple files into a list and how the filenames are accessed.
The endpoint receives a list of UploadFile objects. Each UploadFile has a filename attribute. The list comprehension collects all filenames into a list, so the output JSON contains the list of uploaded filenames.
Which of the following FastAPI endpoint definitions correctly accepts multiple files for upload?
Remember that to accept multiple files, the parameter should be a list of UploadFile.
Option D correctly uses List[UploadFile] with File(...) to accept multiple files. Option D accepts only one file. Option D uses List[str] which is invalid for files. Option D sets a default None and accepts only one file.
Given this FastAPI endpoint:
from fastapi import FastAPI, UploadFile, File
app = FastAPI()
@app.post('/upload')
async def upload(files: UploadFile = File(...)):
return {'filenames': [file.filename for file in files]}Uploading multiple files causes a TypeError: 'UploadFile' object is not iterable. Why?
from fastapi import FastAPI, UploadFile, File app = FastAPI() @app.post('/upload') async def upload(files: UploadFile = File(...)): return {'filenames': [file.filename for file in files]}
Check the type of the parameter files and how it is used in the list comprehension.
The parameter files is declared as a single UploadFile, not a list. Trying to iterate over it causes a TypeError. To accept multiple files, it should be List[UploadFile].
Consider this FastAPI endpoint that calculates total size of uploaded files:
from fastapi import FastAPI, UploadFile, File
from typing import List
app = FastAPI()
@app.post('/upload')
async def upload(files: List[UploadFile] = File(...)):
total_size = 0
for file in files:
content = await file.read()
total_size += len(content)
return {'total_size': total_size}If a client uploads two files, one with 5 bytes and another with 10 bytes, what will be the returned total_size?
from fastapi import FastAPI, UploadFile, File from typing import List app = FastAPI() @app.post('/upload') async def upload(files: List[UploadFile] = File(...)): total_size = 0 for file in files: content = await file.read() total_size += len(content) return {'total_size': total_size}
Think about how the code reads each file's content and sums their lengths.
The code reads each file's content asynchronously and sums the length of bytes. For files of 5 and 10 bytes, total_size is 15.
Choose the correct statement about handling multiple file uploads in FastAPI.
Consider how FastAPI handles file uploads asynchronously and the role of UploadFile.
Option C is true because UploadFile uses async file handling, allowing multiple files to be processed without blocking. Option C is false; files are not saved automatically. Option C is false; UploadFile is async. Option C is false; one endpoint can handle multiple files.