SiteFactory支持Word上传

 在之前在工作中遇到在富文本编辑器中粘贴图片不能展示的问题,于是各种网上扒拉,终于找到解决方案,在这里感谢一下知乎中众大神以及TheViper。

通过知乎提供的思路找到粘贴的原理,通过TheViper找到粘贴图片的方法。

其原理为一下步骤:

监听粘贴事件;【用于插入图片】

获取光标位置;【记录图片插入位置】

获取剪切板内容;【主要是获取文件】

上传剪切板图片;

在指定光标位置插入图片。

以下是代码部分:

1.获取光标代码部分,大部分都是直接利用TheViper的代码,只是做了简单的修改,在获取光标的位置添加了插件子集document对象,因为直接使用document对象获取不到光标位置

var isSupportRange = typeof document.createRange == 'function';

    var currentRange,

        _parentElement;

    // 获取当前光标多在位置

    function getCurrentRange(target) {

        var selection,

            range;

 

        if (isSupportRange) {

            selection = target.getSelection();

            if (selection.getRangeAt && selection.rangeCount) {

                range = selection.getRangeAt(0);

                _parentElement = range.commonAncestorContainer;

            }

        } else {

            range = target.selection.createRange();

            _parentElement = range.parentElement();

        }

        return range;

    }

 

    function saveSelection(target) {

        currentRange = getCurrentRange(target);

    }

 

    function _restoreSelection() {

        if (!currentRange) {

            return;

        }

 

        var selection,

            range;

 

        if (isSupportRange) {

            selection = document.getSelection();

            selection.removeAllRanges();

            selection.addRange(currentRange);

        } else {

            range = document.selection.createRange();

            range.setEndPoint('EndToEnd', currentRange);

            if (currentRange.text.length === 0) {

                range.collapse(false);

            } else {

                range.setEndPoint('StartToStart', currentRange);

            }

            range.select();

        }

    }

 

    function insertImage(html,target) {

        if (document.selection)

            currentRange.pasteHTML(html);

        else

            target.execCommand("insertImage", false, html);

        saveSelection();

    }

 

2.监听粘贴事件、获取上传图片、上传至服务器并添加至编辑器

CKEDITOR.instances['document-info'].on('instanceReady', function(e) {

    this.document.on('paste', function(event) {

        var target = event.sender.$;

        saveSelection(target);

        var items = event.data.$.clipboardData.items;

        if (!items) {

            return;

        }

        for (var i = items.length - 1; i >= 0; i--) {

            if (items[i].kind == 'file' && items[i].type.indexOf('image/') !== -1) {

                var file = items[i].getAsFile();

                if (file) {

                    if (file.size === 0) {

                        return;

                    }

 

                    var formData = new FormData();

                    formData.append("file", file);

 

                    $.ajax({

                        method: 'POST',

                        url: url,

                        data: formData,

                        processData: false,

                        contentType: false,

                        success: function(response) {

                            var _img_html = response.url;

                            insertImage(_img_html,target);

                        }

                    });

                }

            }

        }

    });

});

优化后的代码变得更加精简

    //手动粘贴

     this.PasteManual = function ()

     {

         if (!this.setuped)

        {

            this.setup_tip(); return;

         }

         if (!this.chrome45 && !_this.edge)

         {

 

            this.app.paste();

         }

         else if (this.chrome45)

         {

            this.app.paste();

         }

         else if(this.edge)

         {

            this.app.paste();

         }

     };

 

前台的引用也非常的简单:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

     <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>

     <title>WordPaster-jsp-ueditor-1.2.6.0</title>

     <script type="text/javascript" src="ueditor.config.js" charset="utf-8"></script>

     <script type="text/javascript" src="ueditor.all.min.js" charset="utf-8"></script>

     <link type="text/css" rel="Stylesheet" href="WordPaster/css/WordPaster.css"/>

    <link type="text/css" rel="Stylesheet" href="WordPaster/js/skygqbox.css" />

    <script type="text/javascript" src="WordPaster/js/json2.min.js" charset="utf-8"></script>

    <script type="text/javascript" src="WordPaster/js/jquery-1.4.min.js" charset="utf-8"></script>

    <script type="text/javascript" src="WordPaster/js/w.edge.js" charset="utf-8"></script>

    <script type="text/javascript" src="WordPaster/js/w.app.js" charset="utf-8"></script>

    <script type="text/javascript" src="WordPaster/js/w.file.js" charset="utf-8"></script>

    <script type="text/javascript" src="WordPaster/js/skygqbox.js" charset="utf-8"></script>

    <script type="text/javascript" src="WordPaster/js/WordPaster.js" charset="utf-8"></script>

</head>

<body>

     <textarea name="后台取值的key" id="myEditor">这里写你的初始化内容</textarea>

     <script type="text/javascript">

        var pasterMgr = new WordPasterManager();

    pasterMgr.Config["PostUrl"] = "http://localhost:8080/WordPaster2UEditor1.4x/upload.jsp"

    pasterMgr.Load();//加载控件

 

        var ue = UE.getEditor('myEditor');

        

         ue.ready(function() {

             //设置编辑器的内容

             ue.setContent('hello');

             //获取html内容,返回: <p>hello</p>

             var html = ue.getContent();

             //获取纯文本内容,返回: hello

             var txt = ue.getContentTxt();

             pasterMgr.SetEditor(ue);

         });

                  

     </script>

</body>

</html>

 

 

数据提交部分需要注意

processData: false,

contentType: false,

这两项需要设置,否则文件不能正常上传

 

该问题没有完美的解决,存在以下疑问,如有想法,请告知。

1.从word中复制图片为rtf格式,不能被保存,并解析图片,有待解决;

2.只能单个文件复制,多个文件复制存在问题。这个问题使用WordPaster插件解决掉了,能够批量上传Word中的所有图片,并且保留Word样式,效果如下:

编辑

服务器能够接收到图片,并进行保存

编辑

编辑器中的图片地址已经全部被替换成了服务器的图片地址,其它的用户也能够正常访问 

在接触该问题前期,错误的解决思路:

1.通过input控件上传,因浏览器安全设置原因,不允许input:file赋值;

2.使用convas将图片转换为base64存储,该处也有问题,传唤base64时,存在跨域问题。

经过这些处理基本上实现了一个完整的Word图片上传插件(WordPaster),能够自动上传剪切板中的图片,能够自动上传Word中的所有图片,使用起来非常的方便

相关资料:官方网站,产品首页,产品介绍,产品比较,控件布署(授权码),控件布署(正式包),控件升级,Windows控件安装,macOS控件安装,Linux-deb控件安装,Linux-rpm控件安装,

在线演示:FCKEditor2x示例,CKEditor3x示例,CKEditor4x示例,KindEditor3x示例,KindEditor4x示例,UEditor1x示例,tinymce3x示例,tinymce4x示例,xheditor1x示例,

配置教程:增加自定义参数,自定义文件字段名称,配置上传地址,授权码配置,文件名称生成规则,提取图片地址,图片地址增加域名,设置缩略图,图片格式和质量,控件监听端口,自定义头(Headers)图片数量限制间距优化,

整合教程:FCKEditor 2.x,CKEditor3.x,CKEditor4.x,KindEditor3.x,KindEditor4.x,UMEditor1.x,UEditor 1.x,UEditor 1.4x,UEditor 1.5x,wangEditor3,tinymce4x,eWebEditor 9x,eWebEditor 12.x,neditor,tinymce 3x,HDwik 5.0Drupal 7.34-ckeditor4.x,Joomla-3.4.7-tinymce 4.x,Joomla3x-ckeditor4.x,WordPress,FoonSunCMS,KesionCMS V8,KesionCMS V9,帝国CMS 7.2,emlog,PHPCMS V9,Z-Blog,Z-Blog-php-ueditor,动易SiteFactory 4.7,动易SiteFactory5.6,动易SiteFactory6.2,dedecms 5.7,dedecms 5.7-ckeditor3x,dedecms 5.7-ueditor,vue-cli-ckeditor5x,vue-cli-ueditor,vue-cli-wangEditor,vue-cli-tinymce5,vue-cli-quill,YouDianCMS 9.4.0,DokuWiki,PHPMyWind-5.6,PbootCMS,question2answer

控件包:cab安装包(x86),cab安装包(x64),crx安装包(45-),xpi安装包,exe安装包,pkg安装包,linux-x86-deb,linux-x86-uos,linux-x86-rpm,linux-arm-deb,linux-arm-uos,linux-arm-rpm,linux-mips-deb,linux-mips-uos,linux-mips-rpm,linux-mips-银河麒麟,数字证书根证书,MathType6.5,

ASP示例:FCKEditor 2x,CKEditor 3x,CKEditor 4x,CuteEditor 6x,KindEditor 3x,KindEditor 4x,TinyMCE 3x,TinyMCE 4x,TinyMCE 5x,UEditor1.4x,UEditor 1.5x,UMEditor 1x,xhEditor 1x,eWebEditor 9x,wangEditor,NEditor,Summernote

ASP.NET示例:FCKEditor2x,FCKEditor2x-ext,CKEditor3x,CKEditor3x-ext,CKEditor4x,CuteEditor 6x,KindEditor 3x,KindEditor 4x,TinyMCE 3x,TinyMCE 4x,TinyMCE 5x,UEditor1.4x,,UEditor1.5x,UMEditor1x,xhEditor1x,eWebEditor9x,wangEditor,NEditor,Summernote,SiteFactory 5.2,SiteFactory 5.6

JSP示例:FCKEditor2x,CKEditor3x,CKEditor4x,CuteEditor6x,KindEditor3x,KindEditor4x,TinyMCE3x,TinyMCE4x,TinyMCE5x,UEditor1.4x,UEditor1.5x,UMEditor1x,xhEditor1x,eWebEditor9x,wangEditor,NEditor,Summernote

PHP示例:FCKEditor2x,CKEditor3x,CKEditor4x,CuteEditor6x,KindEditor3x,KindEditor4x,TinyMCE3x,TinyMCE4x,TinyMCE5x,UEditor1.4x,UEditor1.5x,UMEditor1x,xhEditor1x,wangEditor,NEditor,Summernote,HKwik5.0,WordPress,3.7.1,WordPress插件包,Joomla,3.4.7,Drupal,7.34,帝国CMS,7.x插件包,PHPCMS,v9插件包,dedecms,v5.7插件包,dedecms,v5.7-ueditor插件包,DokuWiki插件包,PHPMyWind,5.6插件包,Z-Blog,1.7x插件包,PbootCMS,3.x插件包

vue-cli示例:vue2-cli-CKEditor5,vue2-cli-UEditor1.4x,vue2-cli-UEditor1.5x,vue2-cli-TinyMCE5,vue2-cli-wangEditor,vue2-cli-KindEditor,vue2-cli-Quillvue3-cli-UEditor1.4x,vue3-cli-UEditor1.5x,vue3-cli-TinyMCE5,vue3-cli-wangEditor

相关问题:WebSocket连接失败,ueditor整合后无法粘贴图片,域名未授权,ueditor粘贴部分文档样式不正确,无插件解决方案,

互联网应用:Word内容一键发布,如何转载到新浪博客,如何转载到网易LOFTER如何转载到CSDN博客如何发布到媒介盒子如何发布到365编辑器转载微信公众号图文如何发布到博客园

posted @ 2022-12-14 10:03  潜水的章鱼  阅读(24)  评论(0编辑  收藏  举报