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 is uploaded without setting enctype?
Consider a Flask form that uploads a file but the form tag does not include
enctype="multipart/form-data". What will happen when the user tries to upload a file?Flask
<form method="POST"> <input type="file" name="file"> <input type="submit"> </form>
Attempts:
2 left
💡 Hint
Think about how browsers send file data in forms.
✗ Incorrect
Without enctype="multipart/form-data", the browser sends form data as URL encoded text, which cannot include file content. So request.files will be empty.
❓ state_output
intermediate2:00remaining
What is the value of
filename after uploading a file?Given this Flask route, what will be the value of
filename if the user uploads a file named photo.png?Flask
from flask import Flask, request app = Flask(__name__) @app.route('/upload', methods=['POST']) def upload(): file = request.files['file'] filename = file.filename return filename
Attempts:
2 left
💡 Hint
The
filename attribute holds the original file name from the client.✗ Incorrect
The file.filename property contains the name of the uploaded file as sent by the browser, which is photo.png in this case.
📝 Syntax
advanced2:00remaining
Which option correctly saves an uploaded file securely?
You want to save an uploaded file to a folder named
uploads safely. Which code snippet correctly prevents directory traversal attacks?Flask
from flask import request from werkzeug.utils import secure_filename import os file = request.files['file'] filename = ??? file.save(os.path.join('uploads', filename))
Attempts:
2 left
💡 Hint
Use a function that sanitizes file names.
✗ Incorrect
secure_filename removes dangerous characters and paths from the filename, preventing directory traversal and other security issues.
🔧 Debug
advanced2:00remaining
Why does this Flask file upload code raise a KeyError?
Examine the code below. When submitting the form with a file, the server raises
KeyError: 'file'. Why?Flask
from flask import Flask, request app = Flask(__name__) @app.route('/upload', methods=['POST']) def upload(): uploaded_file = request.files['file'] return uploaded_file.filename
Attempts:
2 left
💡 Hint
Check the HTML form's input name attribute.
✗ Incorrect
If the file input's name attribute is different from 'file', then request.files['file'] will raise a KeyError.
🧠 Conceptual
expert2:00remaining
What is the main reason to limit allowed file extensions in Flask uploads?
Why should you restrict the types of files users can upload by checking their extensions or content in a Flask app?
Attempts:
2 left
💡 Hint
Think about security risks from arbitrary files.
✗ Incorrect
Allowing any file type can lead to security risks like uploading scripts or executables that run malicious code. Restricting file types helps protect the server and users.