quill富文本自定义上传组件
富文本编辑器quill在上传图片的时候会转换为base64格式,这样后端直接存图片而不是图片地址了,需要修改为存图片地址的方式,在查看github之后发现可以增加一个自定义toolbar来解决
- 增加自定义的toolbar
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <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>
|
- 编写customButtonClick和upload方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| 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']); } } })
} }
|
- 参考资料
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