Challenge - 5 Problems
Flask File Serving Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Flask route when serving an uploaded file?
Consider this Flask route that serves a file from the 'uploads' folder. What will the browser receive when accessing '/files/example.txt'?
Flask
from flask import Flask, send_from_directory app = Flask(__name__) @app.route('/files/<filename>') def serve_file(filename): return send_from_directory('uploads', filename) # Assume 'uploads/example.txt' exists with content 'Hello Flask!'
Attempts:
2 left
💡 Hint
Remember that send_from_directory serves files from a folder relative to the app root.
✗ Incorrect
send_from_directory serves the requested file if it exists in the specified folder. Since 'example.txt' exists in 'uploads', the file content is sent to the browser.
📝 Syntax
intermediate2:00remaining
Which option correctly serves an uploaded image file in Flask?
You want to serve an image file named 'photo.png' stored in the 'uploads' folder. Which Flask route code is correct?
Attempts:
2 left
💡 Hint
Use Flask's built-in functions to serve files safely.
✗ Incorrect
send_from_directory safely serves files from a folder. send_file needs a full path and is less safe here. Reading file content directly returns raw bytes without headers.
🔧 Debug
advanced2:00remaining
Why does this Flask route raise a FileNotFoundError when serving uploaded files?
Examine the code below. Why does accessing '/uploads/myfile.txt' cause a FileNotFoundError even though the file exists?
Flask
from flask import Flask, send_from_directory app = Flask(__name__) @app.route('/uploads/<path:filename>') def uploaded_file(filename): return send_from_directory('uploads', filename) # The 'uploads' folder is inside the project directory, not at root '/'
Attempts:
2 left
💡 Hint
Check the folder path passed to send_from_directory carefully.
✗ Incorrect
Using '/uploads' as an absolute path points to the root directory, which likely does not contain the uploads folder. The correct path should be relative or absolute to the project folder.
❓ state_output
advanced2:00remaining
What is the response status code when serving a non-existent uploaded file?
Given this Flask route, what status code will the client receive if the requested file does not exist?
Flask
from flask import Flask, send_from_directory app = Flask(__name__) @app.route('/files/<filename>') def serve_file(filename): return send_from_directory('uploads', filename) # 'uploads/missing.txt' does not exist
Attempts:
2 left
💡 Hint
Think about what Flask does when a file is missing in send_from_directory.
✗ Incorrect
send_from_directory raises a NotFound exception if the file is missing, which Flask converts to a 404 response.
🧠 Conceptual
expert3:00remaining
Which approach best prevents directory traversal attacks when serving uploaded files in Flask?
You want to serve user-uploaded files safely. Which method best prevents users from accessing files outside the 'uploads' folder?
Attempts:
2 left
💡 Hint
Consider how Flask's send_from_directory handles path traversal attempts.
✗ Incorrect
send_from_directory safely resolves the filename inside the folder and prevents directory traversal. Directly opening or concatenating paths without checks can allow attackers to access files outside the folder.