1、在apps 导包:
from flask_uploads import UploadSet, IMAGES, configure_uploads
import sys
import os

2、在apps 配置
# 图片上传配置
fn = getattr(sys.modules['__main__'], '__file__')
root_path = os.path.abspath(os.path.dirname(fn)) + "/static/upload" # +后面是路径
app.config['UPLOADED_PHOTO_DEST'] = root_path
app.config['UPLOADED_PHOTO_ALLOW'] = IMAGES
photos = UploadSet('PHOTO')
configure_uploads(app, photos)

3、上传到服务器
image = request.files.get('pic')
if image:
image_name = photos.save(image) # 将头像保存到 程序目录下
image_url = '/static/upload/' + image_name # 生成图片保存到地址

4,表单中添加 enctype="multipart/form-data"
<form action="" enctype="multipart/form-data" method="post">

posted on 2019-04-24 20:40  满仟  阅读(89)  评论(0编辑  收藏  举报