Bird
0
0

You want to save an uploaded file asynchronously in the background and then log its size. Which FastAPI approach correctly implements this?

hard🚀 Application Q8 of 15
FastAPI - File Handling
You want to save an uploaded file asynchronously in the background and then log its size. Which FastAPI approach correctly implements this?
ARead file content in the endpoint, pass bytes to background task that saves file and logs size
BPass UploadFile directly to background task to save and log size
CSave file synchronously in endpoint, then log size in background task
DUse Depends to inject file size and save file in background task
Step-by-Step Solution
Solution:
  1. Step 1: Read file content in endpoint

    Reading file content before scheduling background task avoids closed stream issues.
  2. Step 2: Pass bytes to background task

    Background task receives bytes, saves file, and logs size safely.
  3. Step 3: Avoid passing UploadFile directly

    Passing UploadFile causes errors as stream closes after request.
  4. Final Answer:

    Read file content in the endpoint, pass bytes to background task that saves file and logs size -> Option A
  5. Quick Check:

    Preload file content before background processing [OK]
Quick Trick: Preload file bytes before background task to avoid closed streams [OK]
Common Mistakes:
MISTAKES
  • Passing UploadFile directly to background task
  • Logging file size before saving

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes