一、在apps 導包:
from flask_uploads import UploadSet, IMAGES, configure_uploads
import sys
import os
二、在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)
三、上傳到服務器
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">flask