Complete the code to import the Flask class.
from flask import [1]
The Flask class is imported from the flask module to create the app instance.
Complete the code to get the uploaded file from the request.
file = request.files.get('[1]')
The key 'file' is commonly used in HTML forms for file inputs and is accessed in Flask via request.files['file'] or get('file').
Fix the error in saving the uploaded file securely.
filename = secure_filename(file.[1]) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
The uploaded file object has a 'filename' attribute that contains the original file name. This is passed to secure_filename() to sanitize it before saving.
Fill both blanks to check if the uploaded file exists and has an allowed extension.
if file and '.' [1] file.filename and file.filename.rsplit('[2]', 1)[1].lower() in ALLOWED_EXTENSIONS:
The code checks if the filename contains a dot '.' to separate the extension, then splits from the right on '.' to get the extension and checks if it is allowed.
Fill all three blanks to create a Flask route that accepts POST requests and processes file upload.
@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'
The route decorator specifies methods=['POST'] to accept uploads. The function checks if request.method == 'POST' to process the upload. The filename attribute of the file object is used to save the file securely.