📖 Scenario: You are building a simple Flask web app that allows users to upload files. To keep the server safe and efficient, you want to limit the size of files users can upload.
🎯 Goal: Create a Flask app that sets a file size limit of 1 megabyte (1MB) and handles file uploads with this limit enforced.
📋 What You'll Learn
Create a Flask app instance
Set the maximum allowed file size to 1MB using Flask configuration
Write a route to accept file uploads
Add error handling for files that exceed the size limit
💡 Why This Matters
🌍 Real World
Limiting file upload size is important to protect servers from overload and abuse in real web applications.
💼 Career
Understanding how to configure Flask apps and handle file uploads is a common task for backend web developers.
Progress0 / 4 steps
1
Create the Flask app instance
Create a Flask app instance called app using Flask(__name__).
Flask
Hint
Use Flask(__name__) to create the app instance.
2
Set the maximum file size limit
Set the Flask configuration variable MAX_CONTENT_LENGTH to 1 * 1024 * 1024 (which is 1MB) on the app.config dictionary.
Flask
Hint
Use app.config['MAX_CONTENT_LENGTH'] = 1 * 1024 * 1024 to set the limit.
3
Create a file upload route
Create a route /upload that accepts POST requests. Inside the route function called upload_file, get the uploaded file from request.files['file'] and return the string 'File uploaded'.
Flask
Hint
Use @app.route('/upload', methods=['POST']) and get the file with request.files['file'].
4
Add error handling for large files
Import RequestEntityTooLarge from werkzeug.exceptions. Add an error handler for RequestEntityTooLarge on app.errorhandler that returns the string 'File too large' with status code 413.
Flask
Hint
Use @app.errorhandler(RequestEntityTooLarge) to catch large file errors.
Practice
(1/5)
1. What does setting MAX_CONTENT_LENGTH in a Flask app do?
easy
A. Sets the maximum number of files a user can upload.
B. Limits the maximum size of incoming request data, like file uploads.
C. Defines the maximum number of users allowed to connect.
D. Controls the maximum time a request can take to process.
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 B
Quick Check:
MAX_CONTENT_LENGTH limits upload size [OK]
Hint: MAX_CONTENT_LENGTH sets max upload size in bytes [OK]
Common Mistakes:
Confusing size limit with number of files
Thinking it limits request time
Assuming it controls user connections
2. Which of the following is the correct way to set a 2MB file size limit in Flask?
easy
A. app.config['MAX_CONTENT_LENGTH'] = 2 * 1024 * 1024
B. app.MAX_CONTENT_LENGTH = 2 * 1024 * 1024
C. app.config.MAX_CONTENT_LENGTH = 2 * 1024 * 1024
D. app.set_config('MAX_CONTENT_LENGTH', 2 * 1024 * 1024)
Solution
Step 1: Recall Flask config syntax
Flask config values are set using app.config dictionary syntax.