0
0
Flaskframework~10 mins

File upload processing 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'
Arender_template
BRequest
CFlask
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'
Afile
Bfiledata
Cupload
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Using keys like 'upload' or 'data' which don't match the form input name
Trying to get the file from request.form instead of request.files
3fill in blank
hard

Fix the error in saving the uploaded file securely.

Flask
filename = secure_filename(file.[1])
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
Drag options to blanks, or click blank then click option'
Afilename
Bname
Cfilename_raw
Dfile_name
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'name' or 'file_name' which are not valid attributes
Trying to save without securing the filename
4fill in blank
hard

Fill both blanks to check if the uploaded file exists and has an allowed extension.

Flask
if file and '.' [1] file.filename and file.filename.rsplit('[2]', 1)[1].lower() in ALLOWED_EXTENSIONS:
Drag options to blanks, or click blank then click option'
A.
Bendswith
Cstartswith
Din
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'endswith' to check for '.' which is incorrect
Using wrong split character other than '.'
5fill in blank
hard

Fill all three blanks to create a Flask route that accepts POST requests and processes file upload.

Flask
@app.route('/upload', methods=[[1]])
def upload_file():
    if [2] == 'POST':
        file = request.files.get('file')
        if file and allowed_file(file.filename):
            filename = secure_filename(file.[3])
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            return 'File uploaded successfully'
    return 'Upload a file'
Drag options to blanks, or click blank then click option'
A'POST'
B'GET'
Crequest.method
Dfilename
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'GET' instead of 'POST' in methods
Checking request.method against 'GET' instead of 'POST'
Using wrong attribute instead of 'filename' for the file name