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

你好

请看如下视频截图

//img.mukewang.com/szimg/5e6b8a1f0913453013261148.jpg

0
0

编程浪子

2020-03-13

你好

报错已经很明显了。

root_path = app.root_path + config_upload['prefix_path']
字符串 + 字符串可以,后面不能加list。

//img.mukewang.com/szimg/5e6b3cee09a2a4a615740158.jpg

//img.mukewang.com/szimg/5e6b3d2d0936884214660350.jpg

0
2
编程浪子
回复
慕虎7527636
你按照视频写的源码,但是你没有按照视频写 UPLOAD的配置信息
2020-03-13
共2条回复

编程浪子

2020-03-13

你好

请把报错信息完全截图,目前的错误信息灭有足够信息判断

0
1
慕虎7527636
老师好,不知道在回复中怎么添加图片,直接编辑了上面的问题。
2020-03-13
共1条回复

Python Flask构建微信小程序订餐系统(可用于毕设)

微信小程序 + Python Flask 打造订餐系统全栈应用,可用于毕设。

1724 学习 · 1913 问题

查看课程