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