flask-uploads上传插件上传文件

安装插件:
pip install flask-uploads
创建上传文件对象:
from flask_uploads import UploadSet
#在这里限制允许上传类型
from flask_uploads import TEXT,IMAGES
uploads=UploadSet(“UPLOADS”,extensions=TEXT+IMAGES)
flask-uploads上传插件上传文件
导出
from .ext_uploads import uploads
加载三方对象 在app.py
from exts import uploads
from flask_uploads import configure_uploads
from flask_uploads import patch_request_class
flask-uploads上传插件上传文件
configure_uploads(app,uploads)
#配置上传文件的大小,默认64M,size也可以设置None,这时会读取配置文件中的MAX_CONTENT_LENGTH里面的值
#patch_request_class(app,size=None)
patch_request_class(app,210241024)
配置
flask-uploads上传插件上传文件
备注:BASE_DIR=os.path.dirname(os.path.dirname(os.path.abspath(file)))#是工程目录

视图
flask-uploads上传插件上传文件
#使用三方flask-uploads
@myApp.route(’/upfile/’,methods=[“GET”,“POST”])
def upfile():
if request.method==“GET”:
return render_template(“upfile.html”)
else:
for name in request.files:
files=request.files.getlist(name)
for file in files:
uploads.save(file,name=file.filename)
return “上传文件”
flask-uploads上传插件上传文件

flask-uploads上传插件上传文件
flask-uploads上传插件上传文件
处理UploadNotAllowed错误:
from flask_uploads import UploadNotAllowed
flask-uploads上传插件上传文件