Complete the code to import the function that helps secure filenames in Flask.
from werkzeug.utils import [1]
The secure_filename function from werkzeug.utils is used to safely handle filenames in Flask.
Complete the code to secure the uploaded file's name before saving.
filename = [1](uploaded_file.filename)Use secure_filename to clean the uploaded file's name before saving it.
Fix the error in the code to correctly save the uploaded file with a secure filename.
uploaded_file.save(os.path.join(app.config['UPLOAD_FOLDER'], [1]))
The filename must be secured using secure_filename before saving to avoid unsafe file paths.
Fill both blanks to create a dictionary comprehension that maps original filenames to their secure versions.
secured_files = {file: [1](file) for file in files if file.endswith([2])}This comprehension creates a dictionary where keys are original filenames and values are their secured versions, filtering only '.txt' files.
Fill all three blanks to create a Flask route that saves an uploaded file securely.
from flask import Flask, request import os from werkzeug.utils import [1] app = Flask(__name__) app.config['UPLOAD_FOLDER'] = '/uploads' @app.route('/upload', methods=['POST']) def upload_file(): uploaded_file = request.files.get('[2]') if uploaded_file: filename = [3](uploaded_file.filename) uploaded_file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) return 'File saved successfully' return 'No file uploaded'
The secure_filename function is imported and used to secure the filename. The file input field name is 'file'.