quill-editor 2.0 图片上传拦截自定义,非base64

如果不知该如何使用quill-editor 2.0,请移步:https://blog.csdn.net/qq_33548028/article/details/138156795

有时需求是该文章不得超过五千字符,但一张base64图片就很长了,这时需要自己调用上传图片方法拿到返回的url,网上都是1.0的写法了,往固定位置插入一张图,然后把光标后移,这种做法不提倡了,下面介绍2.0写法。

首先,需要安装一个 quill-image-uploader,也可以自己写一个(如果你不嫌费事)。我的版本是  "quill-image-uploader": "^1.3.0"

然后,页面引入,当然,你有可能不需要这么多css。

import { QuillEditor, Quill  } from '@vueup/vue-quill'
import ImageUploader from "quill-image-uploader";
import '@vueup/vue-quill/dist/vue-quill.snow.css';
import '@vueup/vue-quill/dist/vue-quill.core.css';
import '@vueup/vue-quill/dist/vue-quill.bubble.css';

最关键的一步,是一定要引进来的ImageUploader注册到Quill实例上

Quill.register("modules/imageUploader", ImageUploader);

然后在quill-editor绑定的options的modules里面,这样写就行了

modules = {
  imageUploader: {
    upload: async (file) => {
      let filePath = ''
      filePath = await handleImg(file)
      if(filePath){
        return filePath
      }else{
        Quill.format('image', false)
      }
    }
  },
  toolbar: {
    container:[
      ['bold', 'italic', 'underline', 'strike'],
    ...省略
  }
}

下面是对图片上传的处理,不能大于2M啊之类的,可忽略

const handleImg = async(file) => {
  return new Promise((resolve,reject)=>{
  const result = beforeFileUpload(file)
  result.then(async res=>{
    if(res){
      customRequest(file).then(response=>{
        imgUrl.value = response
        resolve(imgUrl.value)
      })
    }else{
      reject()
    }
  },()=>{
    resolve('')
  })

  })
}

let customRequest = async(file) => {
  let imgUrlRes;
  const formData = new FormData();
  formData.append('file', file);

  return new Promise((resolve,reject)=>{
    proxy.$postFile('/upload/uploadImage', formData)
    .then((res) => {
      if (res.code == 0) {
        // message.success('上传成功');
        imgUrlRes = res.data.contentImageUrl
        resolve(imgUrlRes)
      } else {
        message.error(res?.message || '图片上传失败');
        reject()
      }
    }).catch((err) => {
      message.error(err)
      reject()
    });
  })

};

const beforeFileUpload = (file) => {

    // 判断兆数
    const test1 = new Promise((res, rej) => {
      const isSize = file.size > 2 * 1024 * 1024;
      if (isSize) {
        message.error(`上传文件不能超过${2}M!`);
        return rej(false);
      } else {
        return res(true);
      }
    })

    const test3 = new Promise((res, rej) => {

      const strType = file.name.split('.');
      const type = strType[strType?.length - 1];
      const isfileType = type === 'jpeg' || type === 'png' || type === 'jpg' || type === 'pdf';

      if (!isfileType) {
        message.error('请上传.jpg .png .pdf 格式的文件');
        return rej(false);
      } else {
        return res(true);
      }
    })

    return Promise.all([test1, test3]).then(async (result) => {
      if (result.includes(false)) {
        // 校验未通过,提醒检查
        antdMessage.error('请检查图片是否符合要求!');
        return false
      } else {
        // 符合所有要求,可以上传
        return true
      }
    })
};

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现vue-quill-editor配合elementui上组件自定义图片操作,可以使用以下步骤: 1. 在vue-quill-editor中配置图片的方法: ``` <template> <quill-editor v-model="content" ref="myQuillEditor" :options="editorOption"></quill-editor> </template> <script> import { quillEditor } from 'vue-quill-editor' export default { components: { quillEditor }, data() { return { content: '', editorOption: { modules: { toolbar: [ ['bold', 'italic', 'underline', 'strike'], ['blockquote', 'code-block'], [{ 'header': 1 }, { 'header': 2 }], [{ 'list': 'ordered' }, { 'list': 'bullet' }], [{ 'script': 'sub' }, { 'script': 'super' }], [{ 'indent': '-1' }, { 'indent': '+1' }], [{ 'direction': 'rtl' }], [{ 'size': ['small', false, 'large', 'huge'] }], [{ 'header': [1, 2, 3, 4, 5, 6, false] }], [{ 'color': [] }, { 'background': [] }], [{ 'font': [] }], [{ 'align': [] }], ['clean'], ['image'] ], imageUploader: { upload: file => { // 自定义图片 } } }, placeholder: '请输入内容', theme: 'snow' } } } } </script> ``` 在`modules`中添加`imageUploader`属性,用于自定义图片操作,其中`upload`方法就是自定义图片方法。 2. 在自定义图片方法中,使用elementui的上组件进行图片: ``` <template> <el-upload class="avatar-uploader" :action="uploadUrl" :show-file-list="false" :on-success="handleSuccess"> <img v-if="imageUrl" :src="imageUrl" class="avatar"> <i v-else class="el-icon-plus avatar-uploader-icon"></i> </el-upload> </template> <script> export default { data() { return { imageUrl: '', uploadUrl: '/api/upload' } }, methods: { handleSuccess(response) { // 上成功后,获取图片地址,然后插入到编辑器中 const url = response.url const quillEditor = this.$refs.myQuillEditor.quill const range = quillEditor.getSelection(true) const index = range.index + range.length quillEditor.insertEmbed(index, 'image', url) } } } </script> ``` 3. 在handleSuccess方法中,获取上成功后的图片地址,然后通过`quillEditor.insertEmbed`方法将图片插入到编辑器中。 这样,就可以实现vue-quill-editor配合elementui上组件自定义图片操作了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值