0
0
Flaskframework~20 mins

File uploads handling in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File Upload 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 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}"
AThe server silently ignores the file and returns a 200 OK with no content.
BFlask raises a RequestEntityTooLarge exception and returns a 413 error automatically.
CThe file uploads successfully but is truncated to 1 KB.
DFlask saves the full file but logs a warning about the size.
Attempts:
2 left
💡 Hint
Think about what Flask does when the uploaded content exceeds the configured max size.
📝 Syntax
intermediate
2: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
A
filename = secure_filename(file.filename)
file.save(f"uploads/{filename}")
return 'File saved'
B
filename = file.filename
file.save(f"uploads/{filename}")
return 'File saved'
C
filename = secure_filename(file.filename)
file.save(f"/uploads/{filename}")
return 'File saved'
D
filename = secure_filename(file.filename)
file.save(f"uploads/" + file.filename)
return 'File saved'
Attempts:
2 left
💡 Hint
Use the secure_filename function to avoid unsafe filenames.
🔧 Debug
advanced
2: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}"
ABecause request.files is always empty regardless of the form.
BBecause the route does not allow GET method.
CBecause 'file' key is missing in request.files when no file is uploaded.
DBecause the file object does not have a filename attribute.
Attempts:
2 left
💡 Hint
Check what happens if the form does not include a file input named 'file'.
state_output
advanced
2: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')
A'secret_txt'
B'.._.._secret.txt'
C'../../secret.txt'
D'secret.txt'
Attempts:
2 left
💡 Hint
secure_filename removes directory traversal parts for safety.
🧠 Conceptual
expert
2:00remaining
Which statement about Flask file upload handling is true?
Select the correct statement about handling file uploads in Flask.
AThe request.files object contains FileStorage objects representing uploaded files.
BFlask automatically saves uploaded files to disk without calling save().
CYou must manually parse multipart form data to access uploaded files in Flask.
DFlask limits file upload size by default to 1 MB.
Attempts:
2 left
💡 Hint
Think about how Flask exposes uploaded files in the request object.