Chrome+IE默认支持粘贴剪切板中的图片,可是我要发布的文章存在word里面,图片多达数十张,我总不能一张一张复制吧?html
我但愿打开文档doc直接复制粘贴到富文本编辑器,直接发布web
感受这个彷佛很困难,由于Ueditor自己不支持,粘贴后直接就是空白,这里面必定有缘由。服务器
好,开始尝试UMeditor,Chrome只能得到本地路径,没法读取文件。app
https://ueditor.baidu.com/website/umeditor.html(有兴趣能够试试)编辑器
难道就这么失败了?ui
不,可是我意外发现UMeditor居然支持粘贴word中的多张图片(仅支持IE11,不支持IE10如下版本、以及Chrome等)this
切换HTML,会看到你的图片被组织成base64url
nice,机会来了,既然IE支持复制word中的多张图片直接粘贴base64,既然有了base64咱们就有办法上传转图片啦!spa
那么咱们来改造Ueditor,让他支持IE11(总比没得用强吧)code
打开你的ueditor.all.js(1.4.3版本如下行号根据本身使用的版本可能不一样)
一、注释掉14679行(暂时不明确有什么不良影响)
//执行默认的处理 //me.filterInputRule(root);
二、在28725行插入如下代码(若是是使用IE11粘贴会获得base64,先用占位符占位,再逐个把base64专成Blob文件并上传,上传完成再替换为你的img属性src为服务器图片url)
//ie11粘贴图片base64,此处用于上传 if (baidu.editor.browser.ie11above) { var eles = editor.document.getElementsByTagName('img'); var imgs = []; for (var i = 0; i < eles.length; i++) { var a = eles[i]; var src = a.getAttribute('src'); if (src.indexOf('data:image') == 0) { a.setAttribute('width', a.width); a.setAttribute('height', a.height); a.className = 'loadingclass'; a.setAttribute('_src', src); a.setAttribute('src', me.themePath + me.theme + '/images/spacer.gif'); imgs.push(a); } } function parseBlob(data) { var arr = data.split(','), mime = arr[0].match(/:(.*?);/)[1], bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n); while (n--) { u8arr[n] = bstr.charCodeAt(n); } return new Blob([u8arr], { type: mime }); } var fdoc = editor.container.ownerDocument; var div = fdoc.getElementById('ie11up_step'); if (div == null) { div = fdoc.createElement('div'); fdoc.getElementsByClassName('edui-toolbar')[0].appendChild(div); div.up = 0; div.str = '图片上传中...(#/' + imgs.length + ')'; div.style.fontSize = '12px'; } function upNextOne() { if (imgs.length == 0) { div.parentNode.removeChild(div); return; } var a = imgs[0]; imgs = imgs.slice(1, imgs.length); var xhr = new XMLHttpRequest(); var fd = new FormData(); fd.append("upfile", parseBlob((xhr.a = a).getAttribute('_src')), 'paste.png'); xhr.upload.addEventListener("progress", function (a) { }, false); xhr.addEventListener("load", function () { var d = JSON.parse(this.response); if (d && d.state == 'SUCCESS') { this.a.setAttribute('src', d.url); this.a.removeAttribute('_src'); this.a.removeAttribute('class'); div.innerText = div.str.replace('#', ++div.up); upNextOne(); } }, false); xhr.open("POST", editor.getActionUrl('uploadimage')); xhr.send(fd); } if (imgs.length > 0) upNextOne(); }
三、处理ueditor提供的uploadimage方法
大功告成
客户已经使用半年,没有问题,很是有用,很是方便的功能,但愿博客园也改良一下TinyMCE。