已form表单为例,设置enctype=“multipart/form-data”
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>上传文件</title> </head> <body> <form action="" method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit"> </form> </body> </html>服务器
from flask import Flask,request,render_template from werkzeug.utils import secure_filename from os import path app = Flask(__name__,template_folder="templates") @app.route("/sss",methods=["POST","GET"]) def file(): if request.method == "GET": return render_template("upload.html") if request.method == "POST": f = request.files["file"] print(f.filename) print(type(f)) # filename=secure_filename(f.filename) # 这种方法只能识别ascii编码的名字 例如:文件名是 嘿嘿嘿11.png 最终识别到的filename叫做11.png # 如果名字全是中文的话 只能识别文件名为png 这时候存下来 没后缀 # print(filename) f.save(path.join("static/uploads",f.filename)) return "上传成功"