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代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Flask上传图片演示</title>
</head>
<body>
<div>
    <h1>please upload a picture</h1>
   <form action="" enctype='multipart/form-data'method='POST'>
        <input type="file" name="file" id="imageUpload" accept=".png, .jpg, .jpeg"><br>
        <input type="submit" value="shangchuan">
    </form>
</div>
</body>
</html>
发布了17 篇原创文章 · 获赞 0 · 访问量 3232

猜你喜欢

转载自blog.csdn.net/qq_31874075/article/details/88719983
今日推荐