vue-quill-editor自定义图片上传

我们通常都会使用富文本编辑器在后台编辑内容,开发vue当然也少不了,我们通过vue官网的链接可以找到一些资源,或者去github上查找一些开源的编辑器。

我使用的是vue-quill-editor,它的界面很简洁,功能也满足平时的编辑需要,不过于臃肿,但是它默认的图片上传是使用Data URL方式保存到了内容里,这不是我想要的,我相信大部分人也不想这样去保存图片,还好quill提供了如何去自定义按钮事件的demo(03-example.vue),那么我们就可以自己去实现图片的保存方式了。

这里我用的是iview 的上传组件,上传成功后获得图片的地址,在编辑器中显示

先下载vue-quill-editor 然后下面是主要代码



<template>
	<div>

		<quill-editor  ref="myQuillEditor"
		    @change="onEditorChange($event)">
		</quill-editor>		
		<Upload :show-upload-list="false"
			style="display: none;"

ref="up"

name="upfile" :format="accept" :max-size="maxsize" multiple type="drag" :on-exceeded-size="handleMaxSize" :on-success="handleSuccess" :action="imgUrl" > <input :id="refid" /> </Upload> </div> </template> <script> import { quillEditor } from 'vue-quill-editor' import $ from 'jquery' export default { name:'quill', components: { quillEditor }, data() { return { visible: false, src: '', uploadList: [] } }, props:{ imgUrl:{ type:String, }, accept:{ type:Array }, maxsize:{ type:Number }, refid:{ type:String } }, computed: { editor() { return this.$refs.myQuillEditor.quill }, }, mounted() { this.uploadList = this.$refs.up.fileList; this.$refs.myQuillEditor.quill.getModule('toolbar').addHandler('image', this.imgClick) this.$refs.myQuillEditor.quill.getModule('toolbar').addHandler('video', this.imgClick) }, methods: { //点击图片触发事件 imgClick() { var up=this.refid; console.log("up===",up) $('#'+this.refid).click() }, //查看图片 handleView(name) { this.src = 'http://' + name; this.visible = true; }, //删除图片 handleRemove(file) { // 从 upload 实例删除数据 const fileList = this.$refs.up.fileList; this.$refs.up.fileList.splice(fileList.indexOf(file), 1); }, //成功回调 handleSuccess(response, file, fileList) { console.log("返回图片") if(response.code == 500) { this.$Message.error('上传失败!') } else { this.$Message.success('上传成功!') //把图片插入指定的位置 this.editor.insertEmbed(this.editor.getSelection().index, 'image', 'http://'+response.data.picName) } console.log(response) console.log(fileList) console.log(file) }, //文件太大,错误提示 handleMaxSize (file) { this.$Notice.warning({ title: 'Exceeding file size limit', desc: 'File ' + file.name + ' is too large, no more than 100M.' }); console.log('File ' + file.name + ' is too large, no more than 100M.'); }, onEditorChange({editor,html,text}) { this.$emit('quilCon',html) } } }; </script> <style> .info { border-radius: 10px; line-height: 20px; padding: 10px; margin: 10px; background-color: #ffffff; } </style> <style scoped> .demo-upload-list { display: inline-block; width: 60px; height: 60px; text-align: center; line-height: 60px; border: 1px solid transparent; border-radius: 4px; overflow: hidden; background: #fff; position: relative; box-shadow: 0 1px 1px rgba(0, 0, 0, .2); margin-right: 4px; } .demo-upload-list img { width: 100%; height: 100%; } .demo-upload-list:hover .demo-upload-list-cover { display: block; } .demo-upload-list-cover { color: #fff; font-size: 16px; display: none; position: absolute; top: 0; bottom: 0; left: 0; right: 0; background: rgba(0, 0, 0, .6); } </style>
对于vue-quill-editor自定义上传图片,你可以按照以下步骤进行操作: 1. 首先,你需要在你的Vue项目中安装vue-quill-editor依赖包。可以使用npm或者yarn命令来安装: ```bash npm install vue-quill-editor # 或者 yarn add vue-quill-editor ``` 2. 在你需要使用vue-quill-editor的组件中引入依赖: ```vue <template> <div> <quill-editor v-model="content" :options="editorOptions" @image-added="handleImageAdded" ></quill-editor> </div> </template> <script> import { quillEditor } from 'vue-quill-editor' export default { components: { quillEditor }, data() { return { content: '', editorOptions: { // 这里可以配置其他选项 } } }, methods: { handleImageAdded(file) { // 自定义处理上传图片的逻辑 // 这里可以使用AJAX或其他方式将图片上传到服务器,然后将返回的图片地址插入到编辑器中 } } } </script> ``` 在上述代码中,我们通过`@image-added`事件监听图片添加的事件,并触发`handleImageAdded`方法来处理上传图片的逻辑。 3. 实现`handleImageAdded`方法,根据你的需求自定义上传图片的逻辑。你可以使用AJAX或其他方式将图片上传到服务器,并获取返回的图片地址。然后,你可以使用Quill提供的API将图片插入到编辑器中。下面是一个示例: ```javascript methods: { handleImageAdded(file) { const formData = new FormData() formData.append('image', file) // 发送AJAX请求将图片上传到服务器 axios.post('/upload', formData) .then(response => { const imageUrl = response.data.imageUrl // 将图片地址插入到编辑器中 const range = this.$refs.editor.quill.getSelection() this.$refs.editor.quill.insertEmbed(range.index, 'image', imageUrl) }) .catch(error => { console.error('上传图片失败', error) }) } } ``` 在上述代码中,我们通过axios库发送了一个POST请求将图片上传到服务器,并获取返回的图片地址。然后,我们使用Quill提供的`insertEmbed`方法将图片地址插入到编辑器中。 请注意,这只是一个示例,具体的上传图片逻辑可能因你的项目需求而有所不同。你需要根据自己的实际情况进行相应的修改。 希望以上信息能对你有所帮助!如果你还有其他问题,请继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值