Challenge - 5 Problems
File Upload Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when a file larger than the limit is uploaded?
Consider a Flask app configured with
app.config['MAX_CONTENT_LENGTH'] = 1024 (1 KB). What will happen if a user tries to upload a file of 2 KB?Flask
from flask import Flask, request app = Flask(__name__) app.config['MAX_CONTENT_LENGTH'] = 1024 @app.route('/upload', methods=['POST']) def upload(): file = request.files['file'] return f"Received file: {file.filename}"
Attempts:
2 left
💡 Hint
Think about what Flask does when the uploaded content exceeds the configured max size.
✗ Incorrect
Flask automatically raises a RequestEntityTooLarge exception when the uploaded file size exceeds MAX_CONTENT_LENGTH, resulting in a 413 HTTP error response.
📝 Syntax
intermediate2:00remaining
Which code correctly saves an uploaded file securely?
Given a Flask route handling file uploads, which option correctly saves the uploaded file using a secure filename?
Flask
from flask import Flask, request from werkzeug.utils import secure_filename app = Flask(__name__) @app.route('/upload', methods=['POST']) def upload(): file = request.files['file'] # Save file here pass
Attempts:
2 left
💡 Hint
Use the secure_filename function to avoid unsafe filenames.
✗ Incorrect
Option A uses secure_filename to sanitize the filename and saves it in the 'uploads' folder correctly. Option A skips sanitization, which is unsafe. Option A uses an absolute path that may not exist or be writable. Option A mixes secure filename with original filename, which is inconsistent.
🔧 Debug
advanced2:00remaining
Why does this Flask upload code raise a KeyError?
This Flask route raises a KeyError when a POST request without a file is sent. Why?
Flask
from flask import Flask, request app = Flask(__name__) @app.route('/upload', methods=['POST']) def upload(): file = request.files['file'] return f"Uploaded {file.filename}"
Attempts:
2 left
💡 Hint
Check what happens if the form does not include a file input named 'file'.
✗ Incorrect
If the client sends a POST request without a file named 'file', accessing request.files['file'] raises a KeyError because that key does not exist.
❓ state_output
advanced2:00remaining
What is the value of 'filename' after this upload code runs?
Given this Flask code snippet, what will be the value of 'filename' if the uploaded file is named '../../secret.txt'?
Flask
from werkzeug.utils import secure_filename filename = secure_filename('../../secret.txt')
Attempts:
2 left
💡 Hint
secure_filename removes directory traversal parts for safety.
✗ Incorrect
secure_filename strips directory paths and returns only the base filename, so '../../secret.txt' becomes 'secret.txt'.
🧠 Conceptual
expert2:00remaining
Which statement about Flask file upload handling is true?
Select the correct statement about handling file uploads in Flask.
Attempts:
2 left
💡 Hint
Think about how Flask exposes uploaded files in the request object.
✗ Incorrect
Flask provides uploaded files as FileStorage objects inside request.files. You must call save() to store them. Flask does not limit upload size by default and handles multipart parsing internally.