Home >Backend Development >Python Tutorial >python code example for uploading files based on flask
The content of this article is about the code example of python uploading files based on flask. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Upload files
Achieve the goal: How to upload files to the server (save in the specified folder)
# 需要建立当前工作环境下的/static/face/目录,文件会保存在此目录下 import os from flask import Flask,render_template,request import uuid app = Flask(__name__) @app.route('/upload/',methods=['GET','POST']) def upload(): if request.method == 'POST': # 获取到用户上传的文件对象 f = request.files['faceImg'] print(f.filename) # 获取当前项目所在目录位置; basepath = os.path.dirname(__file__) print(basepath) # 拼接路径, 存储文件到static/face/xxxx filename = os.path.join(basepath, 'static/face', f.filename) f.save(filename) return render_template('demo/upload.html', message="上传成功") else: return render_template('demo/upload.html') app.run()
The above is the detailed content of python code example for uploading files based on flask. For more information, please follow other related articles on the PHP Chinese website!