TinyMCE实现word图片自动转存

摘要:
query){var_this=服务;var result={status:if(typeofquery.file==“string”){var img=query.file.match(/file:console.log(img);

由于工作需要必须将word文档内容粘贴到编辑器中使用

但发现word中的图片粘贴后变成了file:///xxxx.jpg这种内容,如果上传到服务器后其他人也访问不了,网上找了很多编辑器发现没有一个能直接解决这个问题

考虑到自己除了工作其他时间基本上不使用windows,因此打算使用nodejs来解决这一问题

发现不管什么编辑器只要将图片转换成base64后就可以直接使用(IE8及一下可能不支持),由于编辑器中添加word文档功能也只是自己用,因此可以忽略这种浏览器了

找了很久,试用了很多编辑器,发现只有ckeditor的功能还算符合我的需求(支持自定义HTML属性)

然后我写了一个监听粘贴事件的操作,用来获取粘贴之后的file:///xxxx.jpg这种路径

<script>

    var service = {

http        : require('http'),

url         : require('url'),

querystring : require('querystring'),

fs          : require('fs'),

config      : {

    timeout : 60000,

    charset : 'utf8',

    port    : 10101,

    host    : '127.0.0.1'

},

router : {

    index : function(res, query){

        res.end('Server is running!');

    },

    check : function(res, query){

        var result = {status: 1, info: 'success'};

        result = JSON.stringify(result);

        if(typeof query.callback == 'string'){

            result = query.callback + '(' + result + ')';

        }

        res.end(result);

    },

    word : function(res, query){

        var _this = service;

        var result = {status: 0, info: 'error'};

        if(typeof query.file == 'string'){

            var img = query.file.match(/file://+(localhost)?(S+.(png|jpg|jpeg|gif|bmp))/i);

            console.log(img);

            if(img){

                var base64 = _this.base64_encode(img[2]);

                result.status = 1;

                result.info = 'data:image/' + img[3] + ';base64,' + base64;

            }

        }

        result = JSON.stringify(result);

        if(typeof query.callback == 'string'){

            result = query.callback + '(' + result + ')';

        }

        res.end(result);

    }

},

start : function(){

    var _this  = this;

    var Server = _this.http.createServer(function (req, res) {

        var URL = _this.url.parse(req.url);

        var receive = [];

        var router = null;

        switch(URL.pathname){

            case '/word':

                router = _this.router.word;

                break;

            case '/check':

                router = _this.router.check;

                break;

            default:

                router = _this.router.index;

        }

        req.setEncoding(_this.config.charset);

        req.addListener('data', function(data) {

            receive.push(data);

        });

        res.writeHead(200, {'Content-Type': 'text/plain'});

        res.on("close",function(){

            console.log("res closed");

        });

        req.on("close",function(){

            console.log("req closed");

        });

        req.addListener('end', function() {

            router(res, _this.querystring.parse(URL.query));

        });

    });

    Server.listen(_this.config.port, _this.config.host, 1024);

    Server.setTimeout(_this.config.timeout, function(cli){

        cli.end('timeout ');

    });

    console.log('Server running at http://' + _this.config.host + ':' + _this.config.port);

},

//base64

base64_encode : function(file){

    var bitmap = this.fs.readFileSync(file);

    return new Buffer(bitmap).toString('base64');

}

};

service.start();

</script>

将以上代码保存为一个word.js文件

然后执行 node word.js就会自动创建一个http服务了

这个时候我们在编辑器中使用jsonp获取到处理完的图片数据替换原来的file:///xxxxxx.jpg路径就搞定了

处理word图片批量上传的代码

TinyMCE实现word图片自动转存第1张

其它的业务逻辑参数代码

TinyMCE实现word图片自动转存第2张

当然也可以将以上代码打包成一个本地执行文件去给不懂电脑的人使用就行了(具体方法我这里就不说了)

前台引用的代码

TinyMCE实现word图片自动转存第3张

下面是实现后的效果,能够自动上传Word中的所有图片,并且有进度条显示

TinyMCE实现word图片自动转存第4张

所有图片都能够保存在服务器中,而且支持分布式图片存储

TinyMCE实现word图片自动转存第5张

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

TinyMCE实现word图片自动转存第6张

详细内容可以参考这篇文章:http://blog.ncmem.com/wordpress/2019/07/30/ckeditor%e7%b2%98%e8%b4%b4word/

讨论群:223813913

免责声明:文章转载自《TinyMCE实现word图片自动转存》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇python学习笔记十七:base64及md5编码使用mysqldump导入不成功解决方法下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

easyuitabs(选项卡)

配置好easyui环境 1.笔记: tabs(选项卡) class: //<div style="700px;height:auto"></div> 是否可以关闭:closable:true 选项卡标签的宽度:data-options="tabWidth:200" 下拉菜单: <div id="mm">   <di...

vue3.0用vue-awesome-swiper

使用新版vue-awesome-swiper出现了一些bug vue-awesome-swiper组件pagination小圆点不显示问题 不会自动播放 swiper没有css 参考如下配置 基本使用方法 1.安装 cnpm install vue-awesome-swiper --save 2.引用 /*全局引入*/ import VueAwes...

权限控制管理(用户与菜单项的关联)

GO -- Description: <修改模块权限>-- =============================================ALTER PROCEDURE [dbo].[st_MES_ChgModuleAuth]--1.添加菜单的所具有的权限(对应程序清单的编辑/添加部分。)( @ModuleCode varchar(...

微信网页开发,如何在H5页面中设置分享的标题,内容以及缩略图

前言 最近的需求是做一个移动端H5的长屏广告页,最后需要在微信分享的时候修改文案以及带上图片,这个实现起来也不是很复杂。 实现步骤 先绑定域名,先登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”。由于该自定义分享需要借助微信的jssdk来实现,所以这一步是必须的。 引入微信的jssdk,在需要调用JS接口的页面引入如下JS文件,...

前端开发编辑器(notepad++、sublime text)

1、Notepad++ 正则替换:   如<td>第三节</td>   替换成<td><input type="text" value="第三节" /></td>   可以查找:第(.)节,替换为:<input type="text" value="第(1)节" />。 2、 Note...

weex-iOS集成

weex-iOS集成 weex只是刚刚起步,还存在一些bug,有些功能还有待完善和提高.但是其使用起来还是可以节省些时间. 这里我们说说如何把weex集成到我们的iOS项目中 1. 下载weex源代码 git clone https://github.com/alibaba/weex.git 2. 把根目录下的/ios/sdk整个目录拷贝到项目中...