request.files
前端代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="" method="post" enctype="multipart/form-data"> 用户名:<input type="text" name="username"> 密码:<input type="password" name="password"> <input type="file" name="my_file"> <input type="submit" value="登录"> </form> </body> </html>
后端代码
from flask import Flask,render_template,request app = Flask(__name__) @app.route('/login',methods=["POST","GET"]) def login(): if request.method == "GET": return render_template("login.html") if request.method == "POST": username = request.form.get("username") password = request.form.get("password") if username == "henry" and password == "123456": print(request.files) # ImmutableMultiDict([('my_file', <FileStorage: 'click点击事件.gif' ('image/gif')>)]) files = request.files.get("my_file") # files是FileStorage类型 注意:不是文件句柄 print(files.filename) # click点击事件.gif 【文件名字】 files.save(files.filename) # FileStorage类型调用save方法保存 return "200 ok" else: return "404" if __name__ == '__main__': app.run("0.0.0.0",9876)