php 使用 wangeditor3 图片上传

就在最近,公司让我写一个后台,其中用到了富文本编辑器。自从这个富文本的出现 我就慢慢的进入了一个坑,起初不知道用什么编辑器好,看了好多好多,最后选择了。这个 wangeditor3。我的认为这个富文本很干净,还不少功能。javascript

选择了编辑器 我就慢慢的走进了坑的道理,一步一个坎。接下来就是看代码了。php

这个是wangeditor,选择一个本身喜欢的版本。我用的是3的java

https://github.com/wangfupeng1988/wangEditor/releasesgit

定义一个富文本编辑器github

而后富文本就出现了json

而后就是图片上传代码 首先要在js中配置点东西。服务器

<script>
        var E = window.wangEditor; var editor = new E('#elm1'); editor.customConfig.uploadImgServer = "uploads.php";  // 上传图片到服务器
        editor.customConfig.uploadFileName = "file";      //文件名称 也就是你在后台接受的 参数值
        editor.customConfig.uploadImgHeaders = {    //header头信息 
            'Accept': 'text/x-json' } // 将图片大小限制为 3M
        editor.customConfig.uploadImgMaxSize = 3 * 1024 * 1024   //默认为5M
        editor.customConfig.uploadImgShowBase64 = false;   // 使用 base64 保存图片 // editor.customConfig.customAlert = function (info) { //本身设置alert错误信息 // // info 是须要提示的内容 // alert('自定义提示:' + '图片上传失败,请从新上传') // };
        editor.customConfig.debug = true; //是否开启Debug 默认为false 建议开启 能够看到错误 // editor.customConfig.debug = location.href.indexOf('wangeditor_debug_mode=1') > 0; // 同上 二选一 //图片在编辑器中回显
        editor.customConfig.uploadImgHooks = { error: function (xhr, editor) { alert("2:" + xhr + "请查看你的json格式是否正确,图片并无上传"); // 图片上传出错时触发 若是是这块报错 就说明文件没有上传上去,直接看本身的json信息。是否正确 // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象
            }, fail: function (xhr, editor, result) { // 若是在这出现的错误 就说明图片上传成功了 可是没有回显在编辑器中,我在这作的是在原有的json 中添加了 // 一个url的key(参数)这个参数在 customInsert也用到 // 
                alert("1:" + xhr + "请查看你的json格式是否正确,图片上传了,可是并无回显"); }, success:function(xhr, editor, result){ //成功 不须要alert 固然你能够使用console.log 查看本身的成功json状况 //console.log(result) // insertImg('https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png')
            }, customInsert: function (insertImg, result, editor) { //console.log(result); // 图片上传并返回结果,自定义插入图片的事件(而不是编辑器自动插入图片!!!) // insertImg 是插入图片的函数,editor 是编辑器对象,result 是服务器端返回的结果 // 举例:假如上传图片成功后,服务器端返回的是 {url:'....'} 这种格式,便可这样插入图片:
                insertImg(result.url); } }; editor.customConfig.showLinkImg = true; //是否开启网络图片,默认开启的。
        editor.create() </script>

这些是javascript的配置代码。网络

详细的php代码在个人git里面 有兴趣的能够下载一下~编辑器

https://github.com/wjmGG/wangeditor3UploadForPHP.git函数

这样wangeditor3的图片上传就完成了。

相关文章
相关标签/搜索