0
0
Flaskframework~20 mins

Serving uploaded files in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask File Serving Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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!'
AA 500 Internal Server Error due to missing 'filename' parameter
BA 404 Not Found error because 'send_from_directory' cannot find the file
CThe browser downloads or displays the content of 'example.txt' with text 'Hello Flask!'
DThe browser receives a JSON response with file metadata
Attempts:
2 left
💡 Hint
Remember that send_from_directory serves files from a folder relative to the app root.
📝 Syntax
intermediate
2: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?
A
@app.route('/image/&lt;filename&gt;')
def image(filename):
    return send_from_directory('uploads', filename, mimetype='image/png')
B
@app.route('/image/&lt;filename&gt;')
def image(filename):
    return send_from_directory('uploads', filename)
C
@app.route('/image/&lt;filename&gt;')
def image(filename):
    return send_file('uploads/' + filename)
D
@app.route('/image/&lt;filename&gt;')
def image(filename):
    return open('uploads/' + filename).read()
Attempts:
2 left
💡 Hint
Use Flask's built-in functions to serve files safely.
🔧 Debug
advanced
2: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 '/'
ABecause '/uploads' is an absolute path pointing to root, but the folder is relative to the project directory
BBecause 'send_from_directory' cannot serve files with 'path' converter in route
CBecause the filename parameter is missing in the route
DBecause Flask requires files to be served only from 'static' folder
Attempts:
2 left
💡 Hint
Check the folder path passed to send_from_directory carefully.
state_output
advanced
2: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
A403 Forbidden
B500 Internal Server Error
C200 OK with empty content
D404 Not Found
Attempts:
2 left
💡 Hint
Think about what Flask does when a file is missing in send_from_directory.
🧠 Conceptual
expert
3: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?
AUse send_from_directory with a fixed folder path and a filename parameter without any path separators
BUse open() with the filename directly concatenated to the uploads folder path
CUse send_file with a full path constructed by joining uploads folder and filename without validation
DUse os.system to call 'cat' on the filename inside uploads folder
Attempts:
2 left
💡 Hint
Consider how Flask's send_from_directory handles path traversal attempts.