Complete the code to import the correct FastAPI class.
from fastapi import [1] app = [1]()
The FastAPI class is imported to create the app instance.
Complete the code to define a POST endpoint that accepts a file upload.
@app.post("/upload") async def upload_file(file: [1]): return {"filename": file.filename}
The UploadFile type is used to receive uploaded files in FastAPI.
Fix the error in the code to correctly declare the file parameter with File dependency.
from fastapi import UploadFile, File @app.post("/upload") async def upload_file(file: UploadFile = [1]): return {"filename": file.filename}
The File() function is used as a dependency to tell FastAPI this parameter is a file upload.
Fill both blanks to read the uploaded file content as bytes and return its size.
@app.post("/upload") async def upload_file(file: UploadFile = File()): content = await file.[1]() return {"size": len(content) [2] 0}
Use read() to get file bytes asynchronously. Adding 0 returns the size as is.
Fill all three blanks to save the uploaded file content to disk with the original filename.
import shutil @app.post("/upload") async def upload_file(file: UploadFile = File()): with open([1], "wb") as buffer: shutil.[2](file.[3], buffer) return {"filename": file.filename}
Open a file with the uploaded filename, then use shutil.copyfileobj(file.file, buffer) to efficiently copy the uploaded file content to disk.