flask学习——把图片上传到指定文件夹中

记录一下实现过程和代码

from flask import Flask,render_template,request,redirect,url_for
from werkzeug.utils import secure_filename
import os

app = Flask(__name__)

@app.route('/uppload', methods=['POST', 'GET'])
def upload():
	if request.method == 'POST':
		f = request.files['file']
		basepath = os.path.dirname(__file__)
		upload_path = os.path.join(basepath, 'static/images',secure_filename(f.filename))
		f.save(upload_path)
		return redirect(url_for('upload'))#重定向到页面
	return render_template('upload.html')


if __name__ == '__main__':
	app.run(debug=True)

html代码




    
    Flask上传图片演示


please upload a picture


 

你可能感兴趣的:(flask学习——把图片上传到指定文件夹中)