全部
常见问题
产品动态
精选推荐

WangEditor编辑器 复制粘贴方式上传图片

管理 管理 编辑 删除

2f088202302161426598747.jpg

本文章向大家介绍WangEditor 提交base64格式图片上传至服务器,主要包括WangEditor 提交base64格式图片上传至服务器使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1. wangEditor官网

2.引入安装

友情提示:多商户,PRO,多店版,陀螺匠的项目找到wangEditor的组件参考修改可直接使用。
// 安装
npm i wangeditor --save

// 使用
import E from "wangeditor"
this.editor = new E(this.$refs['wang-editor']);

3.使用

定义上传图片,先转base64,转blob,上传服务器
mounted() {
    this.createEditor();
},
methods: {
  createEditor() {
      let _this = this;
      const menuKey = 'alertMenuKey';
      const html = 'alertHtml';
      this.editor = new E(this.$refs['wang-editor']);

      this.editor.menus.extend(menuKey, AlertMenu);
      this.editor.menus.extend(html, HtmlMenu);
      this.editor.config.menus = this.editor.config.menus.concat(html);
      this.editor.config.menus = this.editor.config.menus.concat(menuKey);
      this.editor.config.uploadImgFromMedia = function () {
        _this.getimg();
      };
      // this.editor.config.uploadVideoHeaders = _this.header;
      this.editor.config.height = 600;
      this.editor.config.menus = [
        'alertHtml',
        'head',
        'bold',
        'fontSize',
        'fontName',
        'italic',
        'underline',
        'strikeThrough',
        'indent',
        'lineHeight',
        'foreColor',
        'backColor',
        'link',
        'list',
        // "todo",
        'justify',
        'quote',
        'image',
        'alertMenuKey',
        // "table",
        'code',
        'splitLine',
      ];
      this.editor.config.customUploadImg = function (files, insert) {
        // 上传代码返回结果之后,将图片插入到编辑器中
        _this.filesToBase64(files);
      };
      // 配置全屏功能按钮是否展示
      //   this.editor.config.showFullScreen = false
      this.editor.config.uploadImgShowBase64 = true;
      //   this.editor.config.uploadImgAccept = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp']
      this.editor.config.zIndex = 0;
      //   this.editor.config.uploadImgMaxSize = this.uploadSize * 1024 * 1024
      this.editor.config.compatibleMode = () => {
        // 返回 true 表示使用兼容模式;返回 false 使用标准模式
        return true;
      };
      this.editor.config.onchange = (newHtml) => {
        this.newHtml = newHtml;
        this.$emit('editorContent', newHtml);
      };
      this.editor.config.onchangeTimeout = 300; // change后多久更新数据

      this.editor.create();
    },
    filesToBase64(files) {
      let _this = this;
      files.map((item) => {
        var reader = new FileReader();
        reader.onload = function (event) {
          var base64_str = event.target.result;
          //div中的img标签src属性赋值,可以直接展示图片
          var bytes = window.atob(base64_str.split(',')[1]);
          var array = [];
          for (let i = 0; i < bytes.length; i++) {
            array.push(bytes.charCodeAt(i));
          }
          var blob = new Blob([new Uint8Array(array)], { type: 'image/jpeg' });
          var formData = new FormData();
          formData.append('file', blob, Date.now() + '.jpg');
          formData.append('filename', 'file');
          fetch('http://xxx.xxx.xxx/upload/image/0/file', {
            method: 'post',
            headers: {
              'X-Token':localStorage.getItem('token'),//token的key和value值根据自己项目进行修改
            },
            body: formData,
          })
            .then((response) => response.json())
            .then((res) => {
              console.log(res.data);
              _this.editor.cmd.do('insertHTML', ``);
              //模板字符串拼接的图片路径层级也根据自己项目返回内容进行调整
            });
        };
        // 传入一个参数对象即可得到基于该参数对象的文本内容
        reader.readAsDataURL(item);
      });
    },
},


附件

index.vue.zip

徐斗明 最后编辑于2023-09-13 16:55:28

快捷回复
回复({{post_count}}) {{!is_user ? '我的回复' :'全部回复'}}
回复从新到旧

{{item.user_info.nickname ? item.user_info.nickname : item.user_name}}

作者 管理员 企业

{{item.floor}}# 同步到gitee 已同步到gitee {{item.is_suggest==1? '取消推荐': '推荐'}}
{{item.floor}}#
{{item.user_info.title}}
附件

{{itemf.name}}

{{item.created_at}}  {{item.ip_address}}
{{item.like_count}}
{{item.showReply ? '取消回复' : '回复'}}
删除
回复
回复

{{itemc.user_info.nickname}}

{{itemc.user_name}}

作者 管理员 企业

回复 {{itemc.comment_user_info.nickname}}

附件

{{itemf.name}}

{{itemc.created_at}}   {{itemc.ip_address}}
{{itemc.like_count}}
{{itemc.showReply ? '取消回复' : '回复'}}
删除
回复
回复
查看更多
回复
回复
5203
{{like_count}}
{{collect_count}}
添加回复 ({{post_count}})

相关推荐

乡关何处 作者
CRMEB定制团队前端开发

回答

207

发布

30

经验

2832

快速安全登录

使用微信扫码登录
{{item.label}} {{item.label}} {{item.label}} 板块推荐 常见问题 产品动态 精选推荐 首页头条 首页动态 首页推荐
加精
取 消 确 定
回复
回复
问题:
问题自动获取的帖子内容,不准确时需要手动修改. [获取答案]
答案:
提交
bug 需求 取 消 确 定

微信登录/注册

切换手机号登录

{{ bind_phone ? '绑定手机' : '手机登录'}}

{{codeText}}
切换微信登录/注册
暂不绑定
CRMEB客服

CRMEB咨询热线 咨询热线

400-8888-794

微信扫码咨询

CRMEB开源商城下载 开源下载 CRMEB官方论坛 帮助文档
返回顶部 返回顶部
CRMEB客服