File size limits help keep your app safe and fast by stopping users from uploading very large files.
File size limits in Flask
app.config['MAX_CONTENT_LENGTH'] = size_in_bytesSet MAX_CONTENT_LENGTH in your Flask app config to limit upload size.
The size is in bytes. For example, 1 MB = 1_000_000 bytes.
app.config['MAX_CONTENT_LENGTH'] = 1_000_000 # 1 MB limit
app.config['MAX_CONTENT_LENGTH'] = 5 * 1024 * 1024 # 5 MB limit
This Flask app limits file uploads to 2 MB. If a user tries to upload a bigger file, Flask stops the request and returns an error automatically.
from flask import Flask, request, abort app = Flask(__name__) # Limit uploads to 2 MB app.config['MAX_CONTENT_LENGTH'] = 2 * 1024 * 1024 @app.route('/upload', methods=['POST']) def upload_file(): file = request.files.get('file') if not file: return 'No file uploaded', 400 # If file is too large, Flask automatically aborts with 413 error return f'File {file.filename} uploaded successfully!' if __name__ == '__main__': app.run(debug=True)
Flask returns a 413 error if the file is too big. You can customize this error by adding an error handler for 413.
Remember to set MAX_CONTENT_LENGTH before running your app.
This limit applies to the whole request size, including all form data.
Set MAX_CONTENT_LENGTH in Flask config to limit file upload size.
Limits help protect your app and improve user experience.
Flask automatically stops requests that exceed the limit with a 413 error.
Practice
MAX_CONTENT_LENGTH in a Flask app do?Solution
Step 1: Understand the role of MAX_CONTENT_LENGTH
This setting defines the maximum size in bytes for incoming request data, such as file uploads.Step 2: Identify what it limits
It limits the size of the request body, preventing large uploads that could harm the app.Final Answer:
Limits the maximum size of incoming request data, like file uploads. -> Option BQuick Check:
MAX_CONTENT_LENGTH limits upload size [OK]
- Confusing size limit with number of files
- Thinking it limits request time
- Assuming it controls user connections
Solution
Step 1: Recall Flask config syntax
Flask config values are set using app.config dictionary syntax.Step 2: Check each option
app.config['MAX_CONTENT_LENGTH'] = 2 * 1024 * 1024 uses app.config['MAX_CONTENT_LENGTH'] correctly. Others use invalid syntax.Final Answer:
app.config['MAX_CONTENT_LENGTH'] = 2 * 1024 * 1024 -> Option AQuick Check:
Use app.config['KEY'] = value [OK]
- Using dot notation instead of dictionary
- Calling non-existent set_config method
- Assigning config directly on app object
app.config['MAX_CONTENT_LENGTH'] = 2 * 1024 * 1024
@app.route('/upload', methods=['POST'])
def upload():
file = request.files['file']
return 'File uploaded!'Solution
Step 1: Understand MAX_CONTENT_LENGTH effect on uploads
Flask rejects requests exceeding this size with a 413 error automatically.Step 2: Analyze the 3MB upload against 2MB limit
3MB is larger than 2MB limit, so Flask blocks the request before reaching the route.Final Answer:
Flask automatically returns a 413 error and blocks the upload. -> Option AQuick Check:
Upload > MAX_CONTENT_LENGTH causes 413 error [OK]
- Assuming upload succeeds anyway
- Thinking app crashes instead of 413 error
- Believing file is truncated silently
app.config['MAX_CONTENT_LENGTH'] = 5 * 1024 * 1024
@app.route('/upload', methods=['POST'])
def upload():
if request.content_length > app.config['MAX_CONTENT_LENGTH']:
return 'File too large', 413
file = request.files['file']
return 'Upload successful'Solution
Step 1: Recall Flask's built-in upload size handling
Flask automatically returns a 413 error if upload exceeds MAX_CONTENT_LENGTH.Step 2: Analyze the manual check in code
The manual check duplicates Flask's behavior and is redundant.Final Answer:
The manual size check is unnecessary; Flask blocks large uploads automatically. -> Option CQuick Check:
Flask auto-blocks large uploads, no manual check needed [OK]
- Thinking content_length is missing
- Believing GET method is required for uploads
- Misunderstanding byte calculation for MAX_CONTENT_LENGTH
Solution
Step 1: Set MAX_CONTENT_LENGTH to desired limit
Setting it to 10MB ensures Flask blocks uploads larger than this automatically.Step 2: Add a Flask error handler for 413 errors
Define a custom error handler to display a friendly message instead of default error.Step 3: Avoid manual size checks or mismatched limits
Manual checks or mismatched limits complicate logic and risk errors.Final Answer:
Set MAX_CONTENT_LENGTH to 10MB and add an error handler for 413 to show a custom page. -> Option DQuick Check:
Use MAX_CONTENT_LENGTH + 413 error handler for custom message [OK]
- Removing MAX_CONTENT_LENGTH and checking manually
- Setting limit higher than needed and manual rejection
- Catching exceptions instead of error handlers
Ready to prove it?
Pass the test and this pattern moves unknown
