标签:main files attr __file__ name 程序 save static 地址
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">
标签:main files attr __file__ name 程序 save static 地址
原文地址:https://www.cnblogs.com/manqian/p/10764820.html