ueditor在flask中的使用技巧
ueditor在flask中的使用技巧
呵哈糊涂 发表于1年前
ueditor在flask中的使用技巧
  • 发表于 1年前
  • 阅读 5
  • 收藏 0
  • 点赞 0
  • 评论 0

【腾讯云】如何购买服务器最划算?>>>   

ueditor是百度前端团队开发的一款功能比较齐全的富文本编辑器,正好项目中需要,参考了一些别人写的例子和官方文档完成了上传功能后台程序的编写:

  • 上传照片/截屏
  • 上传附件
  • 上传涂鸦
  • 上传视频

首先需要的ueditor的config进行配置(具体可参考官方文档):

# 初始化时,返回配置文件给客户端
            result = {"imageActionName": "uploadimage", # 自定义get的名字
                      "snapscreenActionName": "uploadimage",
                      "fileActionName": "uploadfile",
                      "videoActionName": "uploadvideo",
                      "scrawlActionName": "uploadscrawl",
                      "imageFieldName": "upfile",# 定义request名字
                      "fileFieldName": "upfile",
                      "videoFieldName": "upfile",
                      "scrawlFieldName": "upfile",
                      "snapscreenFieldName": "upfile",
                      "imageMaxSize": 2048000,# 定义文件的最大尺寸
                      "fileMaxSize": 51200000,
                      "videoMaxSize": 51200000,
                      "scrawlMaxSize": 2048000,
                      "imageAllowFiles": [".png", ".jpg", ".jpeg", ".gif"], # 定义上传文件的格式

                      "fileAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp", ".html",
                                         ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg",
                                         ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid",
                                         ".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso", ".csv",
                                         ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md",
                                         ".xml"],
                      "videoAllowFiles": [".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg", ".ogg",
                                          ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid"
                                          ],
                      "imageCompressEnable": True,
                      "imageCompressBorder": 1600,
                      "imageInsertAlign": "none",
                      "snapscreenInsertAlign": "none",
                      "imageUrlPrefix": "",
                      "snapscreenUrlPrefix": "",
                      "fileUrlPrefix": "",
                      "videoUrlPrefix": "",
                      "scrawlUrlPrefix": "",
                      "imagePathFormat": "",
                      "filePathFormat": "",
                      "videoPathFormat": ""
                      }

然后是对从前台接收的数据进行保存并返回对应的json

# 涂鸦处理
def upload_scrawl():
    data = base64.b64decode(request.form['upfile'])# 由于涂鸦返回的数据是base64,所以需要使用Python内置的函数base64进行解码
    name = datetime.now().strftime('%Y%m%d%H%M%S')
    filename = name + '.png'# 制作文件名
    open(r"static/upload/scrawl/{}".format(filename), 'wb').write(data) # 将文件存入static/upload/scrawl目录下,这里需要注意解码后的文件为bytes所以要用'wb'
    # 返回需要的json数据
    return {"state": "SUCCESS",
            "url": "/upload/scrawl/{}/".format(filename),
            "title": filename,
            "original": filename,
            }


# 上传视频处理
def upload_video():
    file_storage = request.files['upfile']
    name, extension = os.path.splitext(file_storage.filename) # 将文件剥离成name = xxxxxx extension = .xxx
    name = datetime.now().strftime('%Y%m%d%H%M%S')
    filename = name + extension # 获取的经过格式化的文件名
    file_path = os.path.join(r"static/upload/video/", filename)
    file_storage.save(file_path)
    return {"state": "SUCCESS",
            "url": file_path.replace('static/', '/'),
            "title": file_storage.filename,
            "original": file_storage.filename,
            }


# 上传文件处理
def upload_file():
    file_storage = request.files['upfile']
    name, extension = os.path.splitext(file_storage.filename)
    name = datetime.now().strftime('%Y%m%d%H%M%S')
    filename = name + extension
    file_path = os.path.join(r"static/upload/file/", filename)
    file_storage.save(file_path)
    return {"state": "SUCCESS",
            "url": file_path.replace('static/', '/'),
            "title": file_storage.filename,
            "original": file_storage.filename
            }


# 上传照片包含截屏处理
def upload_image():
    file_storage = request.files['upfile']
    name, extension = os.path.splitext(file_storage.filename)
    name = datetime.now().strftime('%Y%m%d%H%M%S')
    filename = name + extension
    file_path = os.path.join(r"static/upload/image/", filename)
    file_storage.save(file_path)
    return {"state": "SUCCESS",
            "url": file_path.replace('static/', '/'),
            "title": file_storage.filename,
            "original": file_storage.filename,
            "type": extension,
            "size": ""}

 

具体demo可以去我的git上看https://git.oschina.net/hehahutu/myblog.git

标签: Ueditor Python Flask
共有 人打赏支持
粉丝 0
博文 10
码字总数 6480
×
呵哈糊涂
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: