0
0
Flaskframework~10 mins

Saving uploaded files 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 save an uploaded file in Flask.

Flask
file = request.files['file']
file.[1]('uploads/' + file.filename)
Drag options to blanks, or click blank then click option'
Aupload
Bopen
Cwrite
Dsave
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'write' instead of 'save' causes an error because 'write' is not a method of the file object.
Using 'open' is incorrect because it is a built-in function, not a method of the uploaded file.
2fill in blank
medium

Complete the code to check if the uploaded file has a filename before saving.

Flask
if file.[1] != '':
    file.save('uploads/' + file.filename)
Drag options to blanks, or click blank then click option'
Apath
Bfilename
Cfile
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'name' instead of 'filename' will cause an AttributeError.
Checking 'file' or 'path' attributes does not give the filename.
3fill in blank
hard

Fix the error in the code to securely save the uploaded file using Werkzeug's secure_filename.

Flask
from werkzeug.utils import [1]
filename = secure_filename(file.filename)
file.save('uploads/' + filename)
Drag options to blanks, or click blank then click option'
Asecure_filename
Bfilename_secure
Csafe_filename
Dsecure_file
Attempts:
3 left
💡 Hint
Common Mistakes
Importing a non-existent function like 'secure_file' causes ImportError.
Using wrong function names leads to NameError.
4fill in blank
hard

Fill both blanks to save the uploaded file securely in a folder named 'uploads'.

Flask
from werkzeug.utils import [1]
filename = [2](file.filename)
file.save('uploads/' + filename)
Drag options to blanks, or click blank then click option'
Asecure_filename
Bsafe_name
Csanitize_filename
Dclean_filename
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for import and usage causes NameError.
Using made-up function names causes ImportError.
5fill in blank
hard

Fill all three blanks to check if a file was uploaded, secure its filename, and save it.

Flask
if '[1]' in request.files:
    file = request.files['file']
    if file.[2] != '':
        from werkzeug.utils import [3]
        filename = secure_filename(file.filename)
        file.save('uploads/' + filename)
Drag options to blanks, or click blank then click option'
Afile
Bfilename
Csecure_filename
Dupload
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong keys in request.files causes KeyError.
Not checking if filename is empty can cause saving errors.
Importing wrong function names causes ImportError.