enctype="multipart/form-data". What will happen when the user tries to upload a file?<form method="POST"> <input type="file" name="file"> <input type="submit"> </form>
Without enctype="multipart/form-data", the browser sends form data as URL encoded text, which cannot include file content. So request.files will be empty.
filename after uploading a file?filename if the user uploads a file named photo.png?from flask import Flask, request app = Flask(__name__) @app.route('/upload', methods=['POST']) def upload(): file = request.files['file'] filename = file.filename return filename
filename attribute holds the original file name from the client.The file.filename property contains the name of the uploaded file as sent by the browser, which is photo.png in this case.
uploads safely. Which code snippet correctly prevents directory traversal attacks?from flask import request from werkzeug.utils import secure_filename import os file = request.files['file'] filename = ??? file.save(os.path.join('uploads', filename))
secure_filename removes dangerous characters and paths from the filename, preventing directory traversal and other security issues.
KeyError: 'file'. Why?from flask import Flask, request app = Flask(__name__) @app.route('/upload', methods=['POST']) def upload(): uploaded_file = request.files['file'] return uploaded_file.filename
If the file input's name attribute is different from 'file', then request.files['file'] will raise a KeyError.
Allowing any file type can lead to security risks like uploading scripts or executables that run malicious code. Restricting file types helps protect the server and users.
