vue-quill-editor富文本编辑器结合antui上传图片格式base64转成图片路径

1、安装vue-quill-editor

使用命令:npm install vue-quill-editor -S

2、在插入代码

import { quillEditor } from "vue-quill-editor";
import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";
3.写入插件代码
              <quill-editor
                v-model="courseInfo.course_intro"
                :options="editorOption"
                ref="myQuillEditor"
                style="height: 300px"
              ></quill-editor>
              <a-upload
                class="avatar-uploader1"
                name="file"
                :multiple="false"
                :showUploadList="false"
                :before-upload="beforeCourse_introUpload"
                :customRequest="imgBodyCourse_introUpload"
              ></a-upload>

quill-editor:其中 options 为配置内容

      editorOption: {
        placeholder: "请输入课程简介...",
        theme: "snow",
        modules: {
          toolbar: {
            container: toolbarOptions, // 工具栏
            handlers: {
              image: function (value) {
                if (value) {
                  // 触发input框选择图片文件
                  document.querySelector(".avatar-uploader1 input").click();
                } else {
                  this.quill.format("image", false);
                }
              },
            },
          },
        },
      },
const toolbarOptions = [
  [{ color: [] }, { background: [] }],
  ["bold", "italic", "underline", "strike"],
  [{ size: ["small", false, "large"] }],
  [{ list: "ordered" }, { list: "bullet" }],
  ["link", "image"],
  [{ align: [] }],
];

a-upload:before-upload 为文件上传校验

// 课程介绍图片上传前校验
    beforeCourse_introUpload(file) {
      const isJpgOrPng =
        file.type === "image/jpeg" || file.type === "image/png";
      if (!isJpgOrPng) {
        this.$message.error("图片格式错误,请上传jpg或png格式");
      }
      const isLt2M = file.size / 1024 / 1024 < 10;
      if (!isLt2M) {
        this.$message.error("图片大小要小于10MB!");
      }
      return isJpgOrPng && isLt2M;
    },

a-upload:customRequest 为文件覆盖上传路径,自定义上传

// 上传新闻图片
    imgBodyCourse_introUpload(file) {
      const formData = new FormData();
      formData.append("Files", file.file);
      formData.append(
        "SavePath",
        `course/banner/${this.$route.params.courseId}`
      );
      this.Course_introPicUploadToRemote(formData);
    },

执行上传图片

// 上传图片
    async Course_introPicUploadToRemote(formData) {
      const res = await this.$api.PicUploadToRemote(formData);
      if (res.success) {
        let quill = this.$refs.myQuillEditor.quill;
        // 获取光标所在位置
        let length = quill.getSelection().index;
        // 插入图片  res.info为服务器返回的图片地址
        quill.insertEmbed(length, "image", res.response);
        // 调整光标到最后
        quill.setSelection(length + 1);
      } else {
        this.$message.warning(res.msg);
      }
    },

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值