0
0
FastAPIframework~20 mins

Multiple file uploads in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Multiple File Upload Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when multiple files are uploaded using FastAPI's UploadFile list?

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?

FastAPI
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]}
A{"filenames": ["a.txt", "b.txt", "c.txt"]}
B{"filenames": "a.txt,b.txt,c.txt"}
C{"filenames": ["file1", "file2", "file3"]}
D{"filenames": []}
Attempts:
2 left
💡 Hint

Think about how FastAPI collects multiple files into a list and how the filenames are accessed.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a FastAPI endpoint to accept multiple files?

Which of the following FastAPI endpoint definitions correctly accepts multiple files for upload?

A
@app.post('/upload')
async def upload(files: UploadFile = File(None)):
    return {'count': 0}
B
@app.post('/upload')
async def upload(files: UploadFile = File(...)):
    return {'count': 1}
C
@app.post('/upload')
async def upload(files: List[str] = File(...)):
    return {'count': len(files)}
D
@app.post('/upload')
async def upload(files: List[UploadFile] = File(...)):
    return {'count': len(files)}
Attempts:
2 left
💡 Hint

Remember that to accept multiple files, the parameter should be a list of UploadFile.

🔧 Debug
advanced
2:00remaining
Why does this FastAPI code raise a TypeError when uploading multiple files?

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?

FastAPI
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]}
ABecause File(...) does not accept multiple files.
BBecause files is a single UploadFile, not a list, so it cannot be iterated.
CBecause UploadFile does not have a filename attribute.
DBecause the endpoint is missing async keyword.
Attempts:
2 left
💡 Hint

Check the type of the parameter files and how it is used in the list comprehension.

state_output
advanced
2:00remaining
What is the value of 'total_size' after uploading multiple files in this FastAPI endpoint?

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?

FastAPI
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}
A{"total_size": 15}
B{"total_size": 2}
C{"total_size": 0}
D{"total_size": 5}
Attempts:
2 left
💡 Hint

Think about how the code reads each file's content and sums their lengths.

🧠 Conceptual
expert
3:00remaining
Which statement about FastAPI multiple file uploads is TRUE?

Choose the correct statement about handling multiple file uploads in FastAPI.

AFastAPI automatically saves all uploaded files to disk unless manually read.
BThe <code>UploadFile</code> objects are synchronous file objects and block the event loop when read.
CUsing <code>List[UploadFile]</code> with <code>File(...)</code> allows receiving multiple files without blocking the event loop.
DFastAPI requires a separate endpoint for each file to handle multiple uploads.
Attempts:
2 left
💡 Hint

Consider how FastAPI handles file uploads asynchronously and the role of UploadFile.