Challenge - 5 Problems
File Size Limits Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when a file larger than the limit is uploaded?
Consider a Flask app with this setting:
This limits uploads to 1MB.
What happens if a user tries to upload a 2MB file?
app.config['MAX_CONTENT_LENGTH'] = 1024 * 1024This limits uploads to 1MB.
What happens if a user tries to upload a 2MB file?
Attempts:
2 left
💡 Hint
Think about how Flask handles requests exceeding the max content length.
✗ Incorrect
Flask automatically raises a RequestEntityTooLarge exception when the uploaded file size exceeds MAX_CONTENT_LENGTH. This results in a 413 HTTP error response.
📝 Syntax
intermediate1:30remaining
Which code correctly sets a 5MB upload limit in Flask?
You want to limit file uploads to 5 megabytes in your Flask app.
Which code snippet correctly sets this limit?
Which code snippet correctly sets this limit?
Attempts:
2 left
💡 Hint
Remember that the limit is in bytes and 1MB = 1024 * 1024 bytes.
✗ Incorrect
The MAX_CONTENT_LENGTH config expects an integer number of bytes. 5MB is 5 times 1024 * 1024 bytes.
🔧 Debug
advanced2:30remaining
Why does this Flask app not block large uploads?
Given this Flask app snippet:
Users report uploading files larger than 2MB without errors.
What is the most likely reason?
app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 2 * 1024 * 1024
@app.route('/upload', methods=['POST'])
def upload():
file = request.files['file']
file.save('/tmp/' + file.filename)
return 'File saved'Users report uploading files larger than 2MB without errors.
What is the most likely reason?
Attempts:
2 left
💡 Hint
Consider how proxies or web servers might affect upload limits.
✗ Incorrect
Flask enforces MAX_CONTENT_LENGTH on the request size it receives. If a proxy or web server in front allows larger uploads, Flask may never see the limit exceeded.
❓ state_output
advanced1:30remaining
What is the value of
request.content_length for a 3MB upload?In a Flask route handling a POST file upload, the user uploads a file exactly 3 megabytes in size.
What will
What will
request.content_length contain?Attempts:
2 left
💡 Hint
Content length is the size in bytes of the request body.
✗ Incorrect
request.content_length returns the size of the request body in bytes, which for 3MB is 3 * 1024 * 1024 = 3145728 bytes.
🧠 Conceptual
expert3:00remaining
How to customize the error response for large file uploads in Flask?
You want your Flask app to return a custom JSON message when a user uploads a file larger than the limit.
Which approach correctly achieves this?
Which approach correctly achieves this?
Attempts:
2 left
💡 Hint
Flask allows custom error handlers for exceptions.
✗ Incorrect
Flask lets you register an error handler for RequestEntityTooLarge that can return any response, including JSON with a 413 status.