Bird
Raised Fist0
Flaskframework~20 mins

File size limits in Flask - Practice Problems & Coding Challenges

Choose your learning style10 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.

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

  1. 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.
  2. Step 2: Identify what it limits

    It limits the size of the request body, preventing large uploads that could harm the app.
  3. Final Answer:

    Limits the maximum size of incoming request data, like file uploads. -> Option B
  4. 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

  1. Step 1: Recall Flask config syntax

    Flask config values are set using app.config dictionary syntax.
  2. Step 2: Check each option

    app.config['MAX_CONTENT_LENGTH'] = 2 * 1024 * 1024 uses app.config['MAX_CONTENT_LENGTH'] correctly. Others use invalid syntax.
  3. Final Answer:

    app.config['MAX_CONTENT_LENGTH'] = 2 * 1024 * 1024 -> Option A
  4. Quick Check:

    Use app.config['KEY'] = value [OK]
Hint: Use app.config['KEY'] to set Flask config [OK]
Common Mistakes:
  • Using dot notation instead of dictionary
  • Calling non-existent set_config method
  • Assigning config directly on app object
3. Given this Flask app code snippet, what happens if a user uploads a 3MB file?
app.config['MAX_CONTENT_LENGTH'] = 2 * 1024 * 1024

@app.route('/upload', methods=['POST'])
def upload():
    file = request.files['file']
    return 'File uploaded!'
medium
A. Flask automatically returns a 413 error and blocks the upload.
B. The upload succeeds and returns 'File uploaded!'
C. The app crashes with a runtime error.
D. The file is accepted but truncated to 2MB.

Solution

  1. Step 1: Understand MAX_CONTENT_LENGTH effect on uploads

    Flask rejects requests exceeding this size with a 413 error automatically.
  2. Step 2: Analyze the 3MB upload against 2MB limit

    3MB is larger than 2MB limit, so Flask blocks the request before reaching the route.
  3. Final Answer:

    Flask automatically returns a 413 error and blocks the upload. -> Option A
  4. Quick Check:

    Upload > MAX_CONTENT_LENGTH causes 413 error [OK]
Hint: Uploads over limit get 413 error automatically [OK]
Common Mistakes:
  • Assuming upload succeeds anyway
  • Thinking app crashes instead of 413 error
  • Believing file is truncated silently
4. Identify the error in this Flask code that tries to limit upload size:
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'
medium
A. The route should allow GET method to upload files.
B. The content_length attribute does not exist on request.
C. The manual size check is unnecessary; Flask blocks large uploads automatically.
D. The MAX_CONTENT_LENGTH value must be set in bytes, not calculated.

Solution

  1. Step 1: Recall Flask's built-in upload size handling

    Flask automatically returns a 413 error if upload exceeds MAX_CONTENT_LENGTH.
  2. Step 2: Analyze the manual check in code

    The manual check duplicates Flask's behavior and is redundant.
  3. Final Answer:

    The manual size check is unnecessary; Flask blocks large uploads automatically. -> Option C
  4. Quick Check:

    Flask auto-blocks large uploads, no manual check needed [OK]
Hint: Flask auto-blocks large uploads; no manual check needed [OK]
Common Mistakes:
  • Thinking content_length is missing
  • Believing GET method is required for uploads
  • Misunderstanding byte calculation for MAX_CONTENT_LENGTH
5. You want to allow file uploads up to 10MB but also want to show a custom error page instead of Flask's default 413 error. Which approach is best?
hard
A. Set MAX_CONTENT_LENGTH to 10MB and catch exceptions in the route to show custom error.
B. Remove MAX_CONTENT_LENGTH and manually check file size in the route.
C. Set MAX_CONTENT_LENGTH to 20MB and reject files over 10MB manually.
D. Set MAX_CONTENT_LENGTH to 10MB and add an error handler for 413 to show a custom page.

Solution

  1. Step 1: Set MAX_CONTENT_LENGTH to desired limit

    Setting it to 10MB ensures Flask blocks uploads larger than this automatically.
  2. Step 2: Add a Flask error handler for 413 errors

    Define a custom error handler to display a friendly message instead of default error.
  3. Step 3: Avoid manual size checks or mismatched limits

    Manual checks or mismatched limits complicate logic and risk errors.
  4. Final Answer:

    Set MAX_CONTENT_LENGTH to 10MB and add an error handler for 413 to show a custom page. -> Option D
  5. Quick Check:

    Use MAX_CONTENT_LENGTH + 413 error handler for custom message [OK]
Hint: Use MAX_CONTENT_LENGTH + 413 error handler for custom message [OK]
Common Mistakes:
  • Removing MAX_CONTENT_LENGTH and checking manually
  • Setting limit higher than needed and manual rejection
  • Catching exceptions instead of error handlers
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard