ckeditor粘贴word

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

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

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

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

然后我写了一个监听粘贴事件的操作,用来获取粘贴之后的file:///xxxx.jpg这种路径
  1. CKEDITOR.instances[editor].on('instanceReady', function () {
  2.     this.on('afterPaste', function(){
  3.         ...
  4.     });
  5. })
复制代码
获取到上面所说的路径后我们就可以借助nodejs读取本地文件的功能将文件转换成base64的格式了

然后我用nodejs写了一个本地http服务,用来处理jsonp请求
  1. var service = {
  2.     http        : require('http'),
  3.     url         : require('url'),
  4.     querystring : require('querystring'),
  5.     fs          : require('fs'),
  6.     config      : {
  7.         timeout : 60000,
  8.         charset : 'utf8',
  9.         port    : 10101,
  10.         host    : '127.0.0.1'
  11.     },
  12.     router : {
  13.         index : function(res, query){
  14.             res.end('Server is running!');
  15.         },
  16.         check : function(res, query){
  17.             var result = {status: 1, info: 'success'};
  18.             result = JSON.stringify(result);
  19.             if(typeof query.callback == 'string'){
  20.                 result = query.callback + '(' + result + ')';
  21.             }
  22.             res.end(result);
  23.         },
  24.         word : function(res, query){
  25.             var _this = service;
  26.             var result = {status: 0, info: 'error'};
  27.             if(typeof query.file == 'string'){
  28.                 var img = query.file.match(/file://+(localhost)?(S+.(png|jpg|jpeg|gif|bmp))/i);
  29.                 console.log(img);
  30.                 if(img){
  31.                     var base64 = _this.base64_encode(img[2]);
  32.                     result.status = 1;
  33.                     result.info = 'data:image/' + img[3] + ';base64,' + base64;
  34.                 }
  35.             }
  36.             result = JSON.stringify(result);
  37.             if(typeof query.callback == 'string'){
  38.                 result = query.callback + '(' + result + ')';
  39.             }
  40.             res.end(result);
  41.         }
  42.     },
  43.     start : function(){
  44.         var _this  = this;
  45.         var Server = _this.http.createServer(function (req, res) {
  46.             var URL = _this.url.parse(req.url);
  47.             var receive = [];
  48.             var router = null;
  49.             switch(URL.pathname){
  50.                 case '/word':
  51.                     router = _this.router.word;
  52.                     break;
  53.                 case '/check':
  54.                     router = _this.router.check;
  55.                     break;
  56.                 default:
  57.                     router = _this.router.index;
  58.             }
  59.             req.setEncoding(_this.config.charset);
  60.             req.addListener('data', function(data) {
  61.                 receive.push(data);
  62.             });
  63.             res.writeHead(200, {'Content-Type': 'text/plain'});
  64.             res.on("close",function(){
  65.                 console.log("res closed");
  66.             });
  67.             req.on("close",function(){
  68.                 console.log("req closed");
  69.             });
  70.             req.addListener('end', function() {
  71.                 router(res, _this.querystring.parse(URL.query));
  72.             });
  73.         });
  74.         Server.listen(_this.config.port, _this.config.host, 1024);
  75.         Server.setTimeout(_this.config.timeout, function(cli){
  76.             cli.end('timeout ');
  77.         });
  78.         console.log('Server running at http://' + _this.config.host + ':' + _this.config.port);
  79.     },
  80.     //base64
  81.     base64_encode : function(file){
  82.         var bitmap = this.fs.readFileSync(file);
  83.         return new Buffer(bitmap).toString('base64');
  84.     }
  85. };
  86. service.start();
复制代码
将以上代码保存为一个word.js文件

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

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

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

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

原文地址:https://www.cnblogs.com/songsu/p/11301986.html