Bird
0
0

Given this FastAPI code snippet, what will be the output when a file is uploaded?

medium📝 component behavior Q13 of 15
FastAPI - File Handling
Given this FastAPI code snippet, what will be the output when a file is uploaded?
from fastapi import FastAPI, UploadFile
app = FastAPI()

@app.post('/upload')
async def upload(file: UploadFile):
    content = await file.read()
    return {'filename': file.filename, 'size': len(content)}
A{'filename': 'example.txt', 'size': 1000} if a 1000-byte file named example.txt is uploaded
BA syntax error because async functions cannot read files
CAn empty dictionary because file.read() returns None
DA runtime error because file.filename is not accessible
Step-by-Step Solution
Solution:
  1. Step 1: Understand async file reading

    The code uses await file.read() to read the uploaded file content asynchronously.
  2. Step 2: Check returned dictionary

    The function returns the filename and the size of the content in bytes, so for a 1000-byte file named example.txt, it returns that info.
  3. Final Answer:

    {'filename': 'example.txt', 'size': 1000} if a 1000-byte file named example.txt is uploaded -> Option A
  4. Quick Check:

    Async read + filename = correct output [OK]
Quick Trick: Async read returns bytes; filename is accessible [OK]
Common Mistakes:
MISTAKES
  • Thinking async can't read files
  • Assuming file.read() returns None
  • Believing file.filename is inaccessible

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes