0
0
Flaskframework~20 mins

File upload forms 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 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>
AThe file data will not be sent to the server, so <code>request.files</code> will be empty.
BThe file will upload correctly and be accessible in <code>request.files</code>.
CThe server will raise a <code>TypeError</code> when accessing <code>request.files</code>.
DThe browser will automatically add the correct enctype and upload the file.
Attempts:
2 left
💡 Hint
Think about how browsers send file data in forms.
state_output
intermediate
2: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
A"/upload"
B"file"
C"" (empty string)
D"photo.png"
Attempts:
2 left
💡 Hint
The filename attribute holds the original file name from the client.
📝 Syntax
advanced
2: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))
Afilename = os.path.basename(file.filename)
Bfilename = file.filename
Cfilename = secure_filename(file.filename)
Dfilename = file.name
Attempts:
2 left
💡 Hint
Use a function that sanitizes file names.
🔧 Debug
advanced
2: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
AThe form's file input name is not 'file', so <code>request.files</code> has no 'file' key.
BThe route does not allow POST method.
CThe file input is missing the <code>name</code> attribute.
DThe server is missing <code>enctype="multipart/form-data"</code> in the form.
Attempts:
2 left
💡 Hint
Check the HTML form's input name attribute.
🧠 Conceptual
expert
2: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?
ATo reduce the size of the uploaded files and save disk space.
BTo prevent users from uploading files that could execute harmful code on the server or client.
CTo make the upload process faster by only accepting small files.
DTo ensure the file names are unique and avoid overwriting.
Attempts:
2 left
💡 Hint
Think about security risks from arbitrary files.