TypeError: can only concatenate str (not
来源:10-5 editor上传图片和通用上传服务封装

慕虎7527636
2020-03-13
def uploadByFile(file):
config_upload = app.config['UPLOAD']
resp = {'code': 200, 'msg': "操作成功", 'data': {}}
# 存放目录 配置 信息
filename = secure_filename(file.filename)
ext = filename.rsplit(".", 1)[1]
if ext not in config_upload['ext']:
resp['code'] = -1
resp['msg'] = "文件格式有误!!!请重新上传"
return resp
root_path = app.root_path + config_upload['prefix_path']
file_dir = getCurrentDate("%Y%m%d")
save_dir = root_path + file_dir
if not os.path.exists(save_dir):
os.mkdir(save_dir)
os.chmod(save_dir, stat.S_IRWXU | stat.S_IRGRP | stat.S_IRWXO)
file_name = str(uuid.uuid4() ).replace("-", "") + "." + ext
file.save("{0}/{1}".format(save_dir, file_name))
resp['data'] = {
'file_key': file_dir + "/" + file_name
}
return resp
@route_upload.route("/ueditor",methods = [ "GET","POST" ])
def ueditor():
req = request.values
action = req['action'] if 'action' in req else ''
if action == "config":
root_path = app.root_path
config_path = "{0}/web/static/plugins/ueditor/upload_config.json".format( root_path )
with open( config_path,encoding="utf-8" ) as fp:
try:
config_data = json.loads( re.sub( r'/*.**/' ,'',fp.read() ) )
except:
config_data = {}
return jsonify( config_data )
if action == "uploadimage":
return uploadImage()
return "upload"
def uploadImage():
resp = { 'state':'SUCCESS','url':'','title':'','original':'' }
file_target = request.files
upfile = file_target['upfile'] if 'upfile' in file_target else None
if upfile is None:
resp['state'] = "上传失败"
return jsonify(resp)
res = UploadService.uploadByFile( upfile )
if res['code'] != 200:
resp['state'] = "上传失败:" + res['msg']
return jsonify(resp)
resp['url'] = res['data']['file_key']
return jsonify( resp )
UPLOAD = {
# 支持格式 扩展
'ext': ['jpg','bmp','jpeg','PNG','png'],
# 上传目录
'prefix_path': ['/web/static/upload'],
# url地址
'prefix_url': '/status/upload'
}```
写回答
3回答
-
编程浪子
2020-03-13
你好
请看如下视频截图
00 -
编程浪子
2020-03-13
你好
报错已经很明显了。
root_path = app.root_path + config_upload['prefix_path'] 字符串 + 字符串可以,后面不能加list。
022020-03-13 -
编程浪子
2020-03-13
你好
请把报错信息完全截图,目前的错误信息灭有足够信息判断
012020-03-13
相似问题