1.下载百度编辑器(我用的是utf8-php版)
地址:http://ueditor.baidu.com/website/download.html
2.将文件解压放到static文件中,并修改ueditor.config.js文件
var URL = '/static/utf8-php/' || getUEBasePath();
3.在入口文件main.js中引入
import '../static/utf8-php/ueditor.config.js'
import '../static/utf8-php/ueditor.all.min.js'
import '../static/utf8-php/lang/zh-cn/zh-cn.js'
import '../static/utf8-php/ueditor.parse.min.js'
4.在components中写组件ueditor.vue
<template>
<div>
<!--editor的div为富文本的承载容器-->
<div :id='id'></div>
</div>
</template>
<script>
export default {
name:'ue',
props:{
id:String,
config:Object
},
data() {
return {
editor: null,
}
},
mounted() {
// 实例化editor编辑器
this.editor = UE.getEditor(this.id,this.config);
},
methods: {
//getContent() { // 获取内容方法
// console.log(this.editor.getContent())
// return this.editor.getContent();
//}
},
destroyed() {
// 将editor进行销毁
this.editor.destroy();
}
}
</script>
5.在NewContent.vue中运用ueditor.vue组件
<template>
<div>
<div class="info col-md-12 text-center">ueditor编辑器</div>
<div class="col-md-12">
<ueditor :config=config1 :id="ue1"></ueditor>
</div>
<button @click="getUEContent()">获取内容</button>
</div>
</template>
<script>
import ueditor from '../../components/ueditor'
export default{
name:'TeacheNewContent',
components:{
ueditor,
},
data() {
return {
config1: {
// initialFrameWidth: 1600,
initialFrameHeight: 350,
wordCount: false,
},
ue1: "ue1", // 不同编辑器必须不同的id
}
},
methods: {
getUEContent() {
// 获取ueditor值
let content1 = UE.getEditor(this.ue1).getContent();
console.log(content1)
}
},
}
</script>
到这一步还不能上传文件和图片。
6.上传图片和文件
我是用的flask后端,不过道理都是相通的,在flask中配置一个接口,这个接口用于ueditor上传之用。
1)将ueditor文件放到flask的static中,可以只要php文件夹
2)在app文件夹下编辑uploader.py文件
flask目录
# -*- coding: utf-8 -*-
import os
import re
import json
import base64
import random
import urllib
import datetime
from flask import url_for
from werkzeug.utils import secure_filename
class Uploader:
stateMap = [ # 上传状态映射表,国际化用户需考虑此处数据的国际化
"SUCCESS", # 上传成功标记,在UEditor中内不可改变,否则flash判断会出错
"文件大小超出 upload_max_filesize 限制",
"文件大小超出 MAX_FILE_SIZE 限制",
"文件未被完整上传",
"没有文件被上传",
"上传文件为空",
]
stateError = {
"ERROR_TMP_FILE": "临时文件错误",
"ERROR_TMP_FILE_NOT_FOUND": "找不到临时文件",
"ERROR_SIZE_EXCEED": "文件大小超出网站限制",
"ERROR_TYPE_NOT_ALLOWED": "文件类型不允许",
"ERROR_CREATE_DIR": "目录创建失败",
"ERROR_DIR_NOT_WRITEABLE": "目录没有写权限",
"ERROR_FILE_MOVE": "文件保存时出错",
"ERROR_FILE_NOT_FOUND": "找不到上传文件",
"ERROR_WRITE_CONTENT": "写入文件内容错误",
"ERROR_UNKNOWN": "未知错误",
"ERROR_DEAD_LINK": "链接不可用",
"ERROR_HTTP_LINK": "链接不是http链接",
"ERROR_HTTP_CONTENTTYPE": "链接contentType不正确"
}
def __init__(self, fileobj, config, static_folder, _type=None):
"""
:param fileobj: FileStorage, Base64Encode Data or Image URL
:param config: 配置信息
:param static_folder: 文件保存的目录
:param _type: 上传动作的类型,base64,remote,其它
"""
self.fileobj = fileobj
self.config = config
self.static_folder = static_folder
self._type = _type
if _type == 'base64':
self.upBase64()
elif _type == 'remote':
self.saveRemote()
else:
self.upFile()
def upBase64(self):
# 处理base64编码的图片上传
img = base64.b64decode(self.fileobj)
self.oriName = self.config['oriName']
self.fileSize = len(img)
self.fileType = self.getFileExt()
self.fullName = self.getFullName()
self.filePath = self.getFilePath()
# 检查文件大小是否超出限制
if not self.checkSize():
self.stateInfo = self.getStateError('ERROR_SIZE_EXCEED')
return
# 检查路径是否存在,不存在则创建
dirname = os.path.dirname(self.filePath)
if not os.path.exists(dirname):
try:
os.makedirs(dirname)
except:
self.stateInfo = self.getStateError('ERROR_CREATE_DIR')
return
elif not os.access(dirname, os.W_OK):
self.stateInfo = self.getStateError('ERROR_DIR_NOT_WRITEABLE')
return
try:
with open(self.filePath, 'wb') as fp:
fp.write(img)
self.stateInfo = self.stateMap[0]
except:
self.stateInfo = self.getStateError('ERROR_FILE_MOVE')
return
def upFile(self):
# 上传文件的主处理方法
self.oriName = self.fileobj.filename
# 获取文件大小
self.fileobj.stream.seek(0, 2)
self.fileSize = self.fileobj.stream.tell()
self.fileobj.stream.seek(0, 0)
self.fileType = self.getFileExt()
self.fullName = self.getFullName()
self.filePath = self.getFilePath()
# 检查文件大小是否超出限制
if not self.checkSize():
self.stateInfo = self.getStateError('ERROR_SIZE_EXCEED')
return
# 检查是否不允许的文件格式
if not self.checkType():
self.stateInfo = self.getStateError('ERROR_TYPE_NOT_ALLOWED')
return
# 检查路径是否存在,不存在则创建
dirname = os.path.dirname(self.filePath)
if not os.path.exists(dirname):
try:
os.makedirs(dirname)
except:
self.stateInfo = self.getStateError('ERROR_CREATE_DIR')
return
elif not os.access(dirname, os.W_OK):
self.stateInfo = self.getStateError('ERROR_DIR_NOT_WRITEABLE')
return
# 保存文件
try:
self.fileobj.save(self.filePath)
self.stateInfo = self.stateMap[0]
except:
self.stateInfo = self.getStateError('ERROR_FILE_MOVE')
return
def saveRemote(self):
_file = urllib.urlopen(self.fileobj)
self.oriName = self.config['oriName']
self.fileSize = 0
self.fileType = self.getFileExt()
self.fullName = self.getFullName()
self.filePath = self.getFilePath()
# 检查文件大小是否超出限制
if not self.checkSize():
self.stateInfo = self.getStateError('ERROR_SIZE_EXCEED')
return
# 检查路径是否存在,不存在则创建
dirname = os.path.dirname(self.filePath)
if not os.path.exists(dirname):
try:
os.makedirs(dirname)
except:
self.stateInfo = self.getStateError('ERROR_CREATE_DIR')
return
elif not os.access(dirname, os.W_OK):
self.stateInfo = self.getStateError('ERROR_DIR_NOT_WRITEABLE')
return
try:
with open(self.filePath, 'wb') as fp:
fp.write(_file.read())
self.stateInfo = self.stateMap[0]
except:
self.stateInfo = self.getStateError('ERROR_FILE_MOVE')
return
def getStateError(self, error):
# 上传错误检查
return self.stateError.get(error, 'ERROR_UNKNOWN')
def checkSize(self):
# 文件大小检测
return self.fileSize <= self.config['maxSize']
def checkType(self):
# 文件类型检测
return self.fileType.lower() in self.config['allowFiles']
def getFilePath(self):
# 获取文件完整路径
rootPath = self.static_folder
filePath = ''
for path in self.fullName.split('/'):
filePath = os.path.join(filePath, path)
return os.path.join(rootPath, filePath)
def getFileExt(self):
# 获取文件扩展名
return ('.%s' % self.oriName.split('.')[-1]).lower()
def getFullName(self):
# 重命名文件
now = datetime.datetime.now()
_time = now.strftime('%H%M%S')
# 替换日期事件
_format = self.config['pathFormat']
_format = _format.replace('{yyyy}', str(now.year))
_format = _format.replace('{mm}', str(now.month))
_format = _format.replace('{dd}', str(now.day))
_format = _format.replace('{hh}', str(now.hour))
_format = _format.replace('{ii}', str(now.minute))
_format = _format.replace('{ss}', str(now.second))
_format = _format.replace('{ss}', str(now.second))
_format = _format.replace('{time}', _time)
# 过滤文件名的非法自负,并替换文件名
_format = _format.replace('{filename}',
secure_filename(self.oriName))
# 替换随机字符串
rand_re = r'\{rand\:(\d*)\}'
_pattern = re.compile(rand_re, flags=re.I)
_match = _pattern.search(_format)
if _match is not None:
n = int(_match.groups()[0])
_format = _pattern.sub(str(random.randrange(10**(n-1), 10**n)), _format)
_ext = self.getFileExt()
return '%s%s' % (_format, _ext)
def getFileInfo(self):
# 获取当前上传成功文件的各项信息
filename = re.sub(r'^/', '', self.fullName)
return {
'state': self.stateInfo,
'url': url_for('static', filename=filename, _external=True),
'title': self.oriName,
'original': self.oriName,
'type': self.fileType,
'size': self.fileSize,
}
3)在flask的入口文件中加上
from app.uploader import Uploader
@app.route('/upload/', methods=['GET', 'POST', 'OPTIONS'])
def upload():
"""UEditor文件上传接口
config 配置文件
result 返回结果
"""
mimetype = 'application/json'
result = {}
action = request.args.get('action')
# 解析JSON格式的配置文件
with open(os.path.join(app.static_folder, 'ueditor', 'php',
'config.json')) as fp:
try:
# 删除 `/**/` 之间的注释
CONFIG = json.loads(re.sub(r'\/\*.*\*\/', '', fp.read()))
except:
CONFIG = {}
if action == 'config':
# 初始化时,返回配置文件给客户端
result = CONFIG
elif action in ('uploadimage', 'uploadfile', 'uploadvideo'):
# 图片、文件、视频上传
if action == 'uploadimage':
fieldName = CONFIG.get('imageFieldName')
config = {
"pathFormat": CONFIG['imagePathFormat'],
"maxSize": CONFIG['imageMaxSize'],
"allowFiles": CONFIG['imageAllowFiles']
}
elif action == 'uploadvideo':
fieldName = CONFIG.get('videoFieldName')
config = {
"pathFormat": CONFIG['videoPathFormat'],
"maxSize": CONFIG['videoMaxSize'],
"allowFiles": CONFIG['videoAllowFiles']
}
else:
fieldName = CONFIG.get('fileFieldName')
config = {
"pathFormat": CONFIG['filePathFormat'],
"maxSize": CONFIG['fileMaxSize'],
"allowFiles": CONFIG['fileAllowFiles']
}
if fieldName in request.files:
field = request.files[fieldName]
uploader = Uploader(field, config, app.static_folder)
result = uploader.getFileInfo()
else:
result['state'] = '上传接口出错'
elif action in ('uploadscrawl'):
# 涂鸦上传
fieldName = CONFIG.get('scrawlFieldName')
config = {
"pathFormat": CONFIG.get('scrawlPathFormat'),
"maxSize": CONFIG.get('scrawlMaxSize'),
"allowFiles": CONFIG.get('scrawlAllowFiles'),
"oriName": "scrawl.png"
}
if fieldName in request.form:
field = request.form[fieldName]
uploader = Uploader(field, config, app.static_folder, 'base64')
result = uploader.getFileInfo()
else:
result['state'] = '上传接口出错'
elif action in ('catchimage'):
config = {
"pathFormat": CONFIG['catcherPathFormat'],
"maxSize": CONFIG['catcherMaxSize'],
"allowFiles": CONFIG['catcherAllowFiles'],
"oriName": "remote.png"
}
fieldName = CONFIG['catcherFieldName']
if fieldName in request.form:
# 这里比较奇怪,远程抓图提交的表单名称不是这个
source = []
elif '%s[]' % fieldName in request.form:
# 而是这个
source = request.form.getlist('%s[]' % fieldName)
_list = []
for imgurl in source:
uploader = Uploader(imgurl, config, app.static_folder, 'remote')
info = uploader.getFileInfo()
_list.append({
'state': info['state'],
'url': info['url'],
'original': info['original'],
'source': imgurl,
})
result['state'] = 'SUCCESS' if len(_list) > 0 else 'ERROR'
result['list'] = _list
else:
result['state'] = '请求地址出错'
result = json.dumps(result)
if 'callback' in request.args:
callback = request.args.get('callback')
if re.match(r'^[\w_]+$', callback):
result = '%s(%s)' % (callback, result)
mimetype = 'application/javascript'
else:
result = json.dumps({'state': 'callback参数不合法'})
res = make_response(result)
res.mimetype = mimetype
res.headers['Access-Control-Allow-Origin'] = '*'
res.headers['Access-Control-Allow-Headers'] = 'X-Requested-With,X_Requested_With'
return res
7)回到vue中,打开ueditor中的ueditor.config.js,修改serverUrl
serverUrl: '服务器ip/upload/'
8)运行程序,大功告成