0
0
Flaskframework~10 mins

File size limits in Flask - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set the maximum allowed file size to 2 megabytes in a Flask app.

Flask
app.config['MAX_CONTENT_LENGTH'] = [1]
Drag options to blanks, or click blank then click option'
A2 * 1024 * 1024
B2048
C2
D1024 * 1024
Attempts:
3 left
💡 Hint
Common Mistakes
Using 2 instead of bytes value
Using 2048 which is kilobytes, not bytes
Using 1024 * 1024 which is 1 MB, not 2 MB
2fill in blank
medium

Complete the code to handle the error when a user uploads a file larger than the limit in Flask.

Flask
@app.errorhandler([1])
def handle_file_too_large(e):
    return 'File too large', 413
Drag options to blanks, or click blank then click option'
A413
BRequestEntityTooLarge
C413 Request Entity Too Large
DRequestEntityTooLargeException
Attempts:
3 left
💡 Hint
Common Mistakes
Using status code 413 instead of exception class
Using incorrect exception class name
Using string with status code and message
3fill in blank
hard

Fix the error in the code to correctly limit file size and handle errors in Flask.

Flask
from flask import Flask, request
from werkzeug.exceptions import [1]

app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 1 * 1024 * 1024

@app.errorhandler(RequestEntityTooLarge)
def file_too_large(e):
    return 'File too big', 413
Drag options to blanks, or click blank then click option'
AFileTooLarge
BRequestEntityTooLargeError
CRequestTooLarge
DRequestEntityTooLarge
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong exception class name causing import errors
Not importing the exception at all
Using a made-up exception name
4fill in blank
hard

Fill both blanks to create a Flask route that rejects files larger than 500 KB and returns a custom message.

Flask
app.config['MAX_CONTENT_LENGTH'] = [1]

@app.errorhandler([2])
def too_large(e):
    return 'File is too large!', 413
Drag options to blanks, or click blank then click option'
A500 * 1024
BRequestEntityTooLarge
CRequestTooLarge
D1024 * 500
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect exception class
Setting size in kilobytes without converting to bytes
Swapping the blanks
5fill in blank
hard

Fill all three blanks to create a Flask app that limits upload size to 3 MB, handles the error, and returns a JSON response.

Flask
from flask import Flask, jsonify
from werkzeug.exceptions import [1]

app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = [2]

@app.errorhandler(RequestEntityTooLarge)
def handle_large_file(e):
    return jsonify({'error': 'File too large'}), [3]
Drag options to blanks, or click blank then click option'
ARequestEntityTooLarge
B3 * 1024 * 1024
C413
DRequestTooLarge
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong exception class
Setting size without converting to bytes
Using wrong HTTP status code