0
0
Flaskframework~10 mins

File uploads handling 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 import the Flask class.

Flask
from flask import [1]
Drag options to blanks, or click blank then click option'
AFlask
Brender_template
CRequest
Dredirect
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Request instead of Flask
Using lowercase flask instead of Flask
2fill in blank
medium

Complete the code to get the uploaded file from the request.

Flask
file = request.files.get('[1]')
Drag options to blanks, or click blank then click option'
Adata
Bfilename
Cupload
Dfile
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'filename' which is a property, not the input name
Using 'upload' or 'data' which are not standard input names
3fill in blank
hard

Fix the error in saving the uploaded file securely.

Flask
filename = [1](file.filename)
file.save(filename)
Drag options to blanks, or click blank then click option'
Aos.path.basename
Bsecure_filename
Cstr
Dfile.filename.upper
Attempts:
3 left
💡 Hint
Common Mistakes
Saving file.filename directly without sanitizing
Using str() which does not sanitize
Using os.path.basename which only strips directories
4fill in blank
hard

Fill both blanks to check if the file is allowed and save it.

Flask
if file and '[1]' in file.filename and file.filename.rsplit('.', 1)[1].lower() [2] ALLOWED_EXTENSIONS:
    filename = secure_filename(file.filename)
    file.save(filename)
Drag options to blanks, or click blank then click option'
A.
Bin
Cnot in
Dstartswith
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'startswith' instead of checking for '.'
Using 'not in' which reverses the logic
5fill in blank
hard

Fill all three blanks to create a dictionary of filenames and their sizes for allowed files.

Flask
files_info = {file.filename: [1] for file in files if '.' in file.filename and file.filename.rsplit('.', 1)[1].lower() [2] ALLOWED_EXTENSIONS and file [3] None}
Drag options to blanks, or click blank then click option'
Alen(file.read())
Bin
Cis not
Dfile.size
Attempts:
3 left
💡 Hint
Common Mistakes
Using file.size which is not a standard attribute
Using 'is' instead of 'is not' for None check