quill富文本编辑器自定义上传图片

quill富文本编辑器自定义上传图片

自定义上传组件

富文本编辑器quill在上传图片的时候会转换为base64格式,这样后端直接存图片而不是图片地址了,需要修改为存图片地址的方式,在查看github之后发现可以增加一个自定义toolbar来解决

  1. 增加自定义的toolbar

 <quill-editor (onEditorCreated)="EditorCreated($event)" [(ngModel)]="data['noticeContent']" formControlName="noticeContent">
                <div quill-editor-toolbar>
                    
                    <span class="ql-formats">

                        <button nz-button type="button" (click)="customButtonClick($event)" class="ql-image">imgage</button>
                        <input class="open-file" type="file" name="file" id="file" 
                        accept="image/png, image/gif, image/jpeg, image/bmp, image/x-icon"
                        style="display: none;" (change)="upload($event)" multiple="false"
                        />
                    </span>
                 

                </div>
            </quill-editor>

  1. 编写customButtonClick和upload方法

  EditorCreated(quill) {
    this.editor = quill;
  }

  customButtonClick(quill){

    var range = this.editor.getSelection(true);
    var  length = range.index;
	//弹出文件选择框调用upload方法
    this.el.nativeElement.querySelector('.open-file').click();

  }
  

  uploadImg(){

    this.file = [];
    let imgFile=window.document.getElementById('file')['files'][0];
    if(imgFile==null){
      return;
    }
    this.file.push(imgFile);

    if (this.file && this.file !== null && this.file.length>=1) {
	 //上传图片
      this.service.imageUpload(this.file).subscribe(res => {
        if (res['body']) {
          if (res['body']['resCode'] === 20000) {
            let imgUrl=res['body']['data'][0]
            if(imgUrl!=null){
              this.editor.insertEmbed(length, 'image',imgUrl);
            }

          }else {
            this.message.create('error', res['body']['resCode']);
        }
      }
    })

  }
}

  1. 参考资料

https://github.com/surmon-china/vue-quill-editor/issues/21
https://github.com/surmon-china/vue-quill-editor/blob/master/examples/03-example.vue#L34

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值