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 does this Flask route return?
Consider this Flask route that sends a file to the user for download. What will the user experience when accessing this route?
Flask
from flask import Flask, send_file app = Flask(__name__) @app.route('/download') def download(): return send_file('example.txt', as_attachment=True)
Attempts:
2 left
💡 Hint
The 'as_attachment=True' argument controls how the file is sent to the browser.
✗ Incorrect
Using send_file with as_attachment=True tells the browser to download the file instead of displaying it.
📝 Syntax
intermediate2:00remaining
Which option correctly sets a custom filename for download?
You want the user to download a file but with a different filename than the original. Which Flask code snippet correctly sets the download filename to 'report.pdf'?
Flask
from flask import send_file # Assume 'data.pdf' exists on server
Attempts:
2 left
💡 Hint
Check the latest Flask parameter name for setting download filename.
✗ Incorrect
In Flask 2.0+, download_name is the correct parameter to set the filename for download.
🔧 Debug
advanced2:00remaining
Why does this Flask file download route raise an error?
This route is intended to send a file for download but raises a TypeError. What is the cause?
Flask
from flask import send_file @app.route('/getfile') def getfile(): return send_file('file.txt', as_attachment=True, download_name=123)
Attempts:
2 left
💡 Hint
Check the type of the download_name parameter.
✗ Incorrect
The download_name parameter must be a string. Passing an integer causes a TypeError.
❓ state_output
advanced2:00remaining
What is the Content-Disposition header value sent by this Flask route?
Given this Flask route, what will be the exact Content-Disposition header value in the HTTP response?
Flask
from flask import send_file @app.route('/dl') def dl(): return send_file('image.png', as_attachment=True, download_name='photo.png')
Attempts:
2 left
💡 Hint
as_attachment=True sets the disposition to attachment.
✗ Incorrect
Using as_attachment=True sets Content-Disposition to attachment with the filename set to download_name.
🧠 Conceptual
expert2:00remaining
Why use send_file over send_from_directory for file downloads?
Which reason best explains why you might choose Flask's send_file instead of send_from_directory when sending a file for download?
Attempts:
2 left
💡 Hint
Think about flexibility in file sources.
✗ Incorrect
send_file can send any file-like object, including in-memory files, while send_from_directory serves files only from a specific folder.