python flask实战订餐系统微信小程序-44实现ueditor文件的成功上传

wechat xuhss 550℃ 0评论

定义上传配置文件base_setting.py

UPLOAD = {
    'ext':[ 'jpg','gif','bmp','jpeg','png' ],
    'prefix_path':'/web/static/upload/',
    'prefix_url':'/static/upload/'
}

order/common/libs/UploadService.py

添加通用的上传代码:

# -*- coding: utf-8 -*-
from werkzeug.utils import secure_filename
from application import app,db
from common.libs.Helper import getCurrentDate
import datetime
import  os,stat,uuid
class UploadService():
    @staticmethod
    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']
        #不使用getCurrentDate创建目录,为了保证其他写的可以用,这里改掉,服务器上好像对时间不兼容
        file_dir = datetime.datetime.now().strftime("%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

Upload.py完善接口并返回

到ef 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)

    ret = UploadService.uploadByFile( upfile )
    if ret['code'] != 200:
        resp['state'] = "上传失败:" + ret['msg']
        return jsonify(resp)

    resp['url'] = ret['data']['file_key']
    return jsonify( resp )

運行可以發現成功上傳

20210825195016 - python flask实战订餐系统微信小程序-44实现ueditor文件的成功上传

但是当我们点击确认后 上传还是失败了 下次课完善

转载请注明:xuhss » python flask实战订餐系统微信小程序-44实现ueditor文件的成功上传

喜欢 (2)

您必须 登录 才能发表评论!