首页 > 其他分享 >CKEditor粘贴图片自动上传到服务器(VUE版)

CKEditor粘贴图片自动上传到服务器(VUE版)

时间:2023-04-13 15:15:37浏览次数:38  
标签:function VUE CKEditor res result router var query 粘贴

 由于工作需要必须将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\n');

    });

    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中的所有图片,并且有进度条显示

 

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

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

更多详细资料可以参考这篇文章:

详细思路及源码

示例下载:

wordpaster-vue3-cli-ueditor1.5wordpaster-vue-ueditor1.5wordpaster-asp.net-ueditor1.5xwordpaster-php-ueditor1xwordpaster-jsp-ueditor1x​

标签:function,VUE,CKEditor,res,result,router,var,query,粘贴
From: https://www.cnblogs.com/songsu/p/17314890.html

相关文章

  • Vuex笔记
    Vuex有state,mutation,actions,getter四种用法如下:1、state(存储数据):state{count:0//全局数据}获取state数据两种方式:this.$store.state.全局数据名称利用辅助函数mapstateImport{mapState}from“vuex”computed:{…mapstate([“全局数据名称”])}2、mutation(更......
  • vue长按事件指令(v-longPress)
    importtype{Directive,App}from'vue';constlongPress:Directive={beforeMount:function(el,binding,vnode,prevVnode){if(typeofbinding.value!=='function'){throw'callbackmustbeafunction';......
  • VUE.JS和NODE.JS构建一个简易的前后端分离静态博客系统(三)
    Edit.vue<template><divid="edit"><ClassicHeader><templatev-slot:left><span>编辑随笔</span></template><templatev-slot:right><el-button@click="......
  • 百度编辑器粘贴图片自动上传到服务器(Java版)
    ​ 如何做到ueditor批量上传word图片?1、前端引用代码<!DOCTYPE html PUBLIC "-//W3C//DTDXHTML1.0Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>......
  • VUE.JS和NODE.JS构建一个简易的前后端分离静态博客系统(二)
    后台管理页面,需要配合NODE.JS搭建的EXPRESS服务器使用。main.jsimportVuefrom'vue'importAppfrom'./App.vue'importrouterfrom'./router'import{Button,Input,Form,Link,Divider,Upload,Dialog,Card,Popover,Messa......
  • Vue.js 独享路由守卫
    视频独享守卫:beforeEnter(to,from,next){ console.log('beforeEnter',to,from) if(to.meta.isAuth){//判断当前路由是否需要进行权限控制 if(localStorage.getItem('school')==='atguigu'){ next() }else{ alert('暂无权限查看') //next(......
  • Vuex module之间调用与读值
    对于模块内部的mutation和getter,接收的第一个参数是模块的局部状态对象state对于模块内部的action,局部状态通过context.state暴露出来,根节点状态则为context.rootState使用全局state和getter,rootState和rootGetters会作为第三和第四参数传入getter,也会通过conte......
  • 基于Java+Springboot+vue网上商品订单转手系统设计和实现
    基于Java+Springboot+vue网上商品订单转手系统设计和实现一、前言介绍:1.1项目摘要传统办法管理信息首先需要花费的时间比较多,其次数据出错率比较高,而且对错误的数据进行更改也比较困难,最后,检索数据费事费力。因此,在计算机上安装网上商品订单转手系统软件来发挥其高效地信息处理......
  • vue中父子组件调用之头像更新
    问题描述:修改图像的功能,要实现的功能是这样的:点击确认按钮,然后连带着上面的头像也更新,是不是很简单,只需要一个刷新就行,但是事实上没这么简单,因为我的这个项目涉及到三个组件 这里面中间是一个子路由,然后上面是一个组件,左边是一个组件  然后这个就涉及到子父组件传值,这......
  • vue路由懒加载
    vue路由懒加载是性能优化考虑的一种策略。假如在router内需要引入一个component文件,importPrevCompfrom'./components/prev-comp'importNextCompfrom'./components/next-comp'这是常规的文件引入方式,这种方式下有用到和没有用到的文件都会被引入,增加资源加载性能消耗......