Challenge - 5 Problems
File Download Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this FastAPI file download endpoint?
Consider this FastAPI endpoint code. What will the client receive when accessing
/download?FastAPI
from fastapi import FastAPI from fastapi.responses import FileResponse app = FastAPI() @app.get('/download') async def download_file(): return FileResponse('example.txt', media_type='text/plain', filename='example.txt')
Attempts:
2 left
💡 Hint
FileResponse sends the file content with headers to trigger download.
✗ Incorrect
FileResponse streams the file content with the specified media type and sets headers so the browser downloads it with the given filename.
📝 Syntax
intermediate2:00remaining
Which option correctly sets a custom filename for download in FastAPI?
You want the client to download a file named 'report.pdf' regardless of the actual file name on disk. Which code snippet achieves this?
FastAPI
from fastapi.responses import FileResponse # Assume 'files/12345.pdf' exists @app.get('/get-report') async def get_report(): # Fill in the return statement
Attempts:
2 left
💡 Hint
Use the filename parameter to set the download name.
✗ Incorrect
The filename parameter in FileResponse sets the name the client sees when downloading, overriding the actual file path name.
🔧 Debug
advanced2:00remaining
Why does this FastAPI file download endpoint cause a 500 Internal Server Error?
Analyze the code below. Why does the server return a 500 error when the endpoint is called?
FastAPI
from fastapi import FastAPI from fastapi.responses import FileResponse app = FastAPI() @app.get('/download') async def download(): file_path = 'static/report.pdf' return FileResponse(path=file_path, media_type='application/pdf', filename='report.pdf')
Attempts:
2 left
💡 Hint
Check if the file path is correct and file exists on the server.
✗ Incorrect
FileResponse raises an error if the file path does not exist, causing a 500 error. The media_type and filename are valid, and async is allowed.
❓ state_output
advanced2:00remaining
What headers are set by FastAPI's FileResponse for file downloads?
Given this endpoint, which HTTP header ensures the browser downloads the file instead of displaying it inline?
FastAPI
from fastapi import FastAPI from fastapi.responses import FileResponse app = FastAPI() @app.get('/file') async def file(): return FileResponse('data.csv', media_type='text/csv', filename='data.csv')
Attempts:
2 left
💡 Hint
Look for the header that controls download behavior in browsers.
✗ Incorrect
FileResponse sets the Content-Disposition header with 'attachment' and filename to prompt download. Other headers control content type or caching but not download behavior.
🧠 Conceptual
expert3:00remaining
How does FastAPI's FileResponse handle large files efficiently?
FastAPI's FileResponse is used to send large files to clients. How does it avoid loading the entire file into memory at once?
Attempts:
2 left
💡 Hint
Think about how servers send big files without freezing or crashing.
✗ Incorrect
FileResponse uses Python's file streaming to send data in chunks, which is memory efficient and allows clients to start receiving data immediately.