vue+ueditor百度富文本框的前后端配置以及相关bug解决方法

前端配置

vue中使用ueditor,可以用已经封装好的vue-ueditor-wrap,配置简单,功能也是十分强大

安装依赖包

npm i vue-ueditor-wrap

下载ueditor

将下载好的ueditor解压至项目static目录

vue+ueditor百度富文本框的前后端配置以及相关bug解决方法_第1张图片

引入VueUeditorWrap组件

//可全局引入也可局部引入

import VueUeditorWrap from 'vue-ueditor-wrap';

components: { VueUeditorWrap, }

绑定数据并修改VueUeditorWrap配置

data () {

return {

contents: '

Vue + UEditor + v-model双向绑定

',

myConfig: {

autoHeightEnabled: false,// 编辑器不自动被内容撑高

initialFrameHeight: 240,// 初始容器高度

initialFrameWidth: '100%',// 初始容器宽度

serverUrl: 'http://35.201.165.105:8000/controller.php',// 上传文件接口

UEDITOR_HOME_URL: '/UEditor/' , // UEditor 资源文件的存放路径

后端配置

如果服务器网站是phpjspasp搭建的,百度提供了demo,请自行参考ueditor文档http://fex.baidu.com/ueditor/

如果用python,参考DjangoUeditor3定制属于我们自己的ueditor服务端接口

我们来打开DjangoUeditor3的仓库https://github.com/twz915/DjangoUeditor3,

拷贝项目源码

新建视图

新建我们的ueditor视图接口ueditor_view.py

拷贝仓库中的DjangoUeditor3/DjangoUeditor/views.py文件内容到ueditor_view.py

新建配置文件

视图中引用了自己的一个setting,包含自己ueditor上传的一些设置,我们也一起拿过来

在我们当前应用中创建settings.py

DjangoUeditor3/DjangoUeditor/settings.py复制到其中

创建接口url

1
2
from app.ueditorviews import get_ueditor_controller
path('controller',get_ueditor_controller)

get_ueditor_controller就是我们的入口

连接前端vue

vue使用ueditor前端配置中的serverUrl配置为我们刚才的接口地址,我这里都在本地测试,端口不同

1
serverUrl: 'http://localhost:8000/controller',

初试接口

点击上传图片无反应

默认请求接口带了一个actioncallback的参数,200接口访问成功,但是却没有取回任何数据,观察下之前我们用到的测试服务接口,是有返回数据的,对方是用的php,我们来看下ueditor_view.py源码

控制台也打印了如下:

1
Cross-Origin Read Blocking (CORB) blocked cross-origin response http://localhost:8000/controller?action=config&callback=bd__editor__ve7qge with MIME type text/html. See https://www.chromestatus.com/feature/5629709824032768 for more details.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@csrf_exempt
def get_ueditor_controller(request):
    """获取ueditor的后端URL地址"""
    action = request.GET.get("action", "")
    reponseAction = {
        "config": get_ueditor_settings,
        "uploadimage": UploadFile,
        "uploadscrawl": UploadFile,
        "uploadvideo": UploadFile,
        "uploadfile": UploadFile,
        "catchimage": catcher_remote_image,
        "listimage": list_files,
        "listfile": list_files
    }
    return reponseAction[action](request)

可以看出来做了一个类似反射的东西,初始连接带了action参数为config,所以对应的把我们给踢到get_ueditor_settings方法中了,继续跟进到这个方法

1
2
3
4
5
6
@csrf_exempt
def get_ueditor_settings(request):
    json_data = json.dumps(
        USettings.UEditorUploadSettings,
        ensure_ascii=False)
    return HttpResponse(json_data, content_type="application/javascript")

我们来打印下json_data

1
{"imageActionName": "uploadimage", "imageMaxSize": 10485760, "imageFieldName": "upfile", "imageUrlPrefix": "http://localhost:8000", "imagePathFormat": "", "imageAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], "scrawlActionName": "uploadscrawl", "scrawlFieldName": "upfile", "scrawlMaxSize": 10485760, "scrawlUrlPrefix": "", "scrawlPathFormat": "", "snapscreenActionName": "uploadimage", "snapscreenPathFormat": "", "snapscreenUrlPrefix": "", "catcherLocalDomain": ["127.0.0.1", "localhost", "img.baidu.com"], "catcherPathFormat": "", "catcherActionName": "catchimage", "catcherFieldName": "source", "catcherMaxSize": 10485760, "catcherAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], "catcherUrlPrefix": "", "videoActionName": "uploadvideo", "videoPathFormat": "", "videoFieldName": "upfile", "videoMaxSize": 102400000, "videoUrlPrefix": "", "videoAllowFiles": [".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg", ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid"], "fileActionName": "uploadfile", "filePathFormat": "", "fileFieldName": "upfile", "fileMaxSize": 204800000, "fileUrlPrefix": "", "fileAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp", ".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", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml"], "imageManagerActionName": "listimage", "imageManagerListPath": "", "imageManagerListSize": 30, "imageManagerAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], "imageManagerUrlPrefix": "", "fileManagerActionName": "listfile", "fileManagerListPath": "", "fileManagerUrlPrefix": "", "fileManagerListSize": 30, "fileManagerAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp", ".tif", ".psd.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", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml", ".exe", ".com", ".dll", ".msi"]}

有返回数据,可以前台没有响应,再仔细观察下除了action还带了一个callback参数,我们还没用到,刷新发下,这个callback的值每次还随机变动,跟原示例返回值比较下,外层少了一个callback(),我们简单来组装下这个参数。

1
2
callback = request.GET.get('callback')
json_data = '%s(%s)' % (callback,json_data)

再次测试,控制台没有警告了,且我们也取到返回值了

1
bd__editor__dzdteh({"imageActionName": "uploadimage", "imageMaxSize": 10485760, "imageFieldName": "upfile", "imageUrlPrefix": "http://localhost:8000", "imagePathFormat": "", "imageAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], "scrawlActionName": "uploadscrawl", "scrawlFieldName": "upfile", "scrawlMaxSize": 10485760, "scrawlUrlPrefix": "", "scrawlPathFormat": "", "snapscreenActionName": "uploadimage", "snapscreenPathFormat": "", "snapscreenUrlPrefix": "", "catcherLocalDomain": ["127.0.0.1", "localhost", "img.baidu.com"], "catcherPathFormat": "", "catcherActionName": "catchimage", "catcherFieldName": "source", "catcherMaxSize": 10485760, "catcherAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], "catcherUrlPrefix": "", "videoActionName": "uploadvideo", "videoPathFormat": "", "videoFieldName": "upfile", "videoMaxSize": 102400000, "videoUrlPrefix": "", "videoAllowFiles": [".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg", ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid"], "fileActionName": "uploadfile", "filePathFormat": "", "fileFieldName": "upfile", "fileMaxSize": 204800000, "fileUrlPrefix": "", "fileAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp", ".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", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml"], "imageManagerActionName": "listimage", "imageManagerListPath": "", "imageManagerListSize": 30, "imageManagerAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], "imageManagerUrlPrefix": "", "fileManagerActionName": "listfile", "fileManagerListPath": "", "fileManagerUrlPrefix": "", "fileManagerListSize": 30, "fileManagerAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp", ".tif", ".psd.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", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml", ".exe", ".com", ".dll", ".msi"]})

测试单图片上传,应该大多数人已经可以成功上传了。

图片上传成功,却提示上传错误,不显示图片

这部分都是跨域问题,解决办法如下:UploadFile函数返回值做修改,添加一个header即可。

1
2
3
response = HttpResponse(json.dumps(return_info, ensure_ascii=False))
response['Access-Control-Allow-Headers'] = '*'
return response

测试多图片

点击上传多张图片按钮会出现以下提示:

 

控制台:

1
Access to XMLHttpRequest at 'http://localhost:8000/controller?action=uploadimage&encode=utf-8' from origin 'http://localhost:8080' has been blocked by CORS policy: Request header field x_requested_with is not allowed by Access-Control-Allow-Headers in preflight response.

 

图片上传失败;跨域问题;而且访问接口请求成功200,请求方法却是options以前没有用到过该方法,百度查了下,大概意思是说这个浏览器自己发的,先试探下服务器允不允许。我在ueditor_view入口函数包括上传函数中都打印请求,很显然什么也没打印出来,真是不知道这个options方法会走到程序的哪里,甚至打算将这个视图写成cbv,走走options函数试试。

后来想到初始使用vuedjango时也出现了该提示,详见:Access-Control-Allow-Origin跨域问题,当时用的是

django-cors-headers,按照文中内容配置好

主要配置:

1
2
3
CORS_ALLOW_HEADERS = [
    'x-requested-with',
]

测试多图片上传,完美

后端配置遇到问题:

在本地调试需带上callback参数,服务器上则需要去掉callback参数

前端富文本隐藏问题:

1.ueditor图片和图片调节框错位

可以通过修改ueditor.all.js的代码来实现调节框位置与图片位置一致。如下
原代码17097行:

‘top’: iframePos.y + imgPos.y - me.editor.document.body.scrollTop - editorPos.y - parseInt(resizer.style.borderTopWidth) + ‘px’;
修改为:

‘top’: iframePos.y + imgPos.y - me.editor.document.documentElement.scrollTop - editorPos.y - parseInt(resizer.style.borderTopWidth) + ‘px’;

2.如何隐藏图片调节框

直接在css文件里设置
#edui1_imagescale{display:none !important;} /去除点击图片后出现的拉伸边框/

3.视频无法播放,格式被自动存为gif

修改ueditor.all.js文件

ueditor.all.js,17769行

html.push(creatInsertStr( vi.url, vi.width || 420,  vi.height || 280, id + i, null, cl, 'image'));

修改为

html.push(creatInsertStr( vi.url, vi.width || 420,  vi.height || 280, id + i, null, cl, 'video'));

7343,7344,7346行,注释掉一下三行代码:

var root = UE.htmlparser(html);

me.filterInputRule(root);

html = root.toHtml();

参考文章:https://www.diandian100.cn/9923e2ee.html

参考文章:https://blog.csdn.net/qq_35405240/article/details/82786590

参考文章:https://blog.csdn.net/eadela/article/details/76264168

 

你可能感兴趣的:(vue+ueditor百度富文本框的前后端配置以及相关bug解决方法)