0
0
Flaskframework~20 mins

File size limits in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File Size Limits Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a file larger than the limit is uploaded?
Consider a Flask app with this setting:
app.config['MAX_CONTENT_LENGTH'] = 1024 * 1024
This limits uploads to 1MB.
What happens if a user tries to upload a 2MB file?
AFlask accepts the file but truncates it to 1MB before saving.
BFlask raises a RequestEntityTooLarge exception and returns a 413 error automatically.
CFlask silently ignores the file and processes the request as if no file was sent.
DFlask crashes and stops the server due to memory overflow.
Attempts:
2 left
💡 Hint
Think about how Flask handles requests exceeding the max content length.
📝 Syntax
intermediate
1: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?
Aapp.config['MAX_CONTENT_LENGTH'] = 5 ** 1024 ** 1024
Bapp.config['MAX_CONTENT_LENGTH'] = 5 * 1000 * 1000
Capp.config['MAX_CONTENT_LENGTH'] = '5MB'
Dapp.config['MAX_CONTENT_LENGTH'] = 5 * 1024 * 1024
Attempts:
2 left
💡 Hint
Remember that the limit is in bytes and 1MB = 1024 * 1024 bytes.
🔧 Debug
advanced
2:30remaining
Why does this Flask app not block large uploads?
Given this Flask app snippet:
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?
AThe MAX_CONTENT_LENGTH config is set correctly but the server is behind a proxy that allows larger uploads.
BThe app does not handle the RequestEntityTooLarge exception, so uploads proceed anyway.
CThe client is not sending the Content-Length header, so Flask cannot enforce the limit.
DThe MAX_CONTENT_LENGTH setting must be set before creating the Flask app instance.
Attempts:
2 left
💡 Hint
Consider how proxies or web servers might affect upload limits.
state_output
advanced
1: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 request.content_length contain?
ANone, because content_length is only set for GET requests
B3000000 (which is 3 * 1000 * 1000 bytes)
C3145728 (which is 3 * 1024 * 1024 bytes)
DRaises an AttributeError because content_length is not available
Attempts:
2 left
💡 Hint
Content length is the size in bytes of the request body.
🧠 Conceptual
expert
3: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?
AUse an errorhandler for RequestEntityTooLarge to return a JSON response with status 413.
BCatch the exception inside the route function and return JSON manually.
COverride the MAX_CONTENT_LENGTH setting with a function that returns JSON on error.
DSet a custom response in app.config['MAX_CONTENT_LENGTH_RESPONSE'] with the JSON message.
Attempts:
2 left
💡 Hint
Flask allows custom error handlers for exceptions.