vue-quill-editor上传图片到服务器

2018-01-13 发布

vue-quill-editor-upload : 实现vue-quill-editor上传图片到服务器

install

  • npm

    npm install vue-quill-editor-upload --save

<template>
  <!-- bidirectional data binding(双向数据绑定) -->
  <quill-editor v-model="content"
                ref="myQuillEditor"
                :options="editorOption">
  </quill-editor>
</template>

<script>
  import {quillRedefine} from 'vue-quill-editor-upload'
  import {quillEditor} from 'vue-quill-editor'
  export default {
    components: {quillEditor, quillRedefine},
    data () {
      return {
        content: '',
        editorOption: {}  // 必须初始化为对象 init  to Object
      }
    },
    created () {
      this.editorOption = quillRedefine(
        {
          // 图片上传的设置
          uploadConfig: {
            action: '',  // 必填参数 图片上传地址
            // 必选参数  res是一个函数,函数接收的response为上传成功时服务器返回的数据
            // 你必须把返回的数据中所包含的图片地址 return 回去
            res: (respnse) => {
              return respnse.info
            },
            name: 'img'  // 图片上传参数名
          }
        }
      )
      console.log(this.editorOption)
    }
  }
</script>

use

You have to install vue-quill-editor first.

请确保您已安装了 vue-quill-editor

import

import {quillRedefine} from ‘vue-quill-editor-upload’

quillRedefine是一个函数

quillRedefine 可接收的所有参数(all params)
 

{
          // 图片上传的设置
          uploadConfig: {
            action: '',  // 必填参数 图片上传地址
            // 必选参数  res是一个函数,函数接收的response为上传成功时服务器返回的数据
            // 你必须把返回的数据中所包含的图片地址 return 回去
            res: (respnse) => {
              return respnse.info
            },
            methods: 'POST',  // 可选参数 图片上传方式  默认为post
            token: sessionStorage.token,  // 可选参数 如果需要token验证,假设你的token有存放在sessionStorage
            name: 'img',  // 可选参数 文件的参数名 默认为img
            size: 500,  // 可选参数   图片限制大小,单位为Kb, 1M = 1024Kb
            accept: 'image/png, image/gif, image/jpeg, image/bmp, image/x-icon',  // 可选参数 可上传的图片格式
            // start: function (){}
            start: () => { },  // 可选参数 接收一个函数 开始上传数据时会触发
            end: () => { },  // 可选参数 接收一个函数 上传数据完成(成功或者失败)时会触发
            success: () => {},  // 可选参数 接收一个函数 上传数据成功时会触发
            error: () => { }  // 可选参数 接收一个函数 上传数据中断时会触发
          },
          // 以下所有设置都和vue-quill-editor本身所对应
          placeholder: '',  // 可选参数 富文本框内的提示语
          theme: '',  // 可选参数 富文本编辑器的风格
          toolOptions: [],  // 可选参数  选择工具栏的需要哪些功能  默认是全部
          handlers: {}  // 可选参数 重定义的事件,比如link等事件
}

  • demo

first

you must to do: :options=“editorOption” to bound Parameters

你必须绑定option :options=“editorOption”

<template>
  <!-- bidirectional data binding(双向数据绑定) -->
  <quill-editor 
                :options="editorOption">
  </quill-editor>
</template>

second

return editorOption

必须在return 中书写editorOPtion 并且设置默认为空对象

 data () {
      return {
        content: '',
        editorOption: {}  // 必须初始化为对象 init  to Object
      }
    }

three

init in created

在created生命周期中生成实际数据

created () {
      this.editorOption = quillRedefine(
        {
          // 图片上传的设置
          uploadConfig: {
            action:  '',  // 必填参数 图片上传地址
            // 必选参数  res是一个函数,函数接收的response为上传成功时服务器返回的数据
            // 你必须把返回的数据中所包含的图片地址 return 回去
            res: (respnse) => {
              return respnse.info  // 这里切记要return回你的图片地址
            }
          }
        }
      )
     // console.log(this.editorOption)
    }

注意事项 (matters need attention)

由于不同的用户的服务器返回的数据格式不尽相同

因此

在uploadConfig中,你必须如下操作

 // 你必须把返回的数据中所包含的图片地址 return 回去
 res: (respnse) => {
    return respnse.info  // 这里切记要return回你的图片地址
 }

比如你的服务器返回的成功数据为

{
code: 200,
starus: true,
result: {
    img: 'http://placehold.it/100x100' // 服务器返回的数据中的图片的地址
 }
}

那么你应该在参数中写为:

 // 你必须把返回的数据中所包含的图片地址 return 回去
 res: (respnse) => {
    return respnse.result.img  // 这里切记要return回你的图片地址
 }

example

完整用例

<template>
  <!-- bidirectional data binding(双向数据绑定) -->
  <quill-editor v-model="content"
                ref="myQuillEditor"
                :options="editorOption">
  </quill-editor>
</template>

<script>
  import {quillRedefine} from 'vue-quill-editor-upload'
  import {quillEditor} from 'vue-quill-editor'
  export default {
    components: {quillEditor, quillRedefine},
    data () {
      return {
        content: '',
        editorOption: {}  // 必须初始化为对象 init  to Object
      }
    },
    created () {
      this.editorOption = quillRedefine(
        {
          // 图片上传的设置
          uploadConfig: {
            action: '',  // 必填参数 图片上传地址
            // 必选参数  res是一个函数,函数接收的response为上传成功时服务器返回的数据
            // 你必须把返回的数据中所包含的图片地址 return 回去
            res: (respnse) => {
              return respnse.info
            },
            methods: 'POST',  // 可选参数 图片上传方式  默认为post
            token: sessionStorage.token,  // 可选参数 如果需要token验证,假设你的token有存放在sessionStorage
            name: 'img',  // 可选参数 文件的参数名 默认为img
            size: 500,  // 可选参数   图片限制大小,单位为Kb, 1M = 1024Kb
            accept: 'image/png, image/gif, image/jpeg, image/bmp, image/x-icon',  // 可选参数 可上传的图片格式
            // start: function (){}
            start: () => {
            },  // 可选参数 接收一个函数 开始上传数据时会触发
            end: () => {
            },  // 可选参数 接收一个函数 上传数据完成(成功或者失败)时会触发
            success: () => {
            },  // 可选参数 接收一个函数 上传数据成功时会触发
            error: () => {
            }  // 可选参数 接收一个函数 上传数据中断时会触发
          },
          // 以下所有设置都和vue-quill-editor本身所对应
          placeholder: '',  // 可选参数 富文本框内的提示语
          theme: '',  // 可选参数 富文本编辑器的风格
          toolOptions: [],  // 可选参数  选择工具栏的需要哪些功能  默认是全部
          handlers: {}  // 可选参数 重定义的事件,比如link等事件
        }
      )
      console.log(this.editorOption)
    }
  }
</script>

上面的代码是我从别的地方搬过来的 下面的代码是我的 可以根据我的代码结合一块看

我认为最重要的一点就是母吆盲目的复制粘贴,因为开发人员写不出来就会急躁,故要将该考虑的都考虑到然后进行操作

<template>
    <div id="articleAdd">
        <div class="search">
            <el-form  label-width="80px">
                <el-form-item label="标题">
                    <el-input clearable placeholder="请输入标题" v-model="form.title"></el-input>
                </el-form-item>

                <el-form-item label="作者">
                    <el-input clearable="clearable" placeholder="请输入作者" v-model="form.author"></el-input>
                </el-form-item>

                <el-form-item label="权重">
                    <el-input clearable="clearable" placeholder="请输入权重" v-model="form.sort"></el-input>
                </el-form-item>

                <el-form-item label="封面">
                    <el-upload class="upload-demo" :action="uploadUrl" :data="prefix" :file-list="coverBox"
                        :on-remove="handleRemove" :on-success="handleAvatarSuccess"
                        list-type="picture">
                        <el-button size="small" type="primary">点击上传</el-button>
                    </el-upload>
                </el-form-item>
                <el-form-item label="上传类型">
                    <el-radio-group v-model="upLoadType">
                        <el-radio :label="1">上传链接</el-radio>
                        <el-radio :label="2">上传文章</el-radio>
                    </el-radio-group>
                </el-form-item>

            </el-form>
            
        </div>

        <!-- 图片上传组件辅助-->
        <el-upload
            class="avatar-uploader"
            :action="uploadUrl" type="drag"
            :data="prefix"
            :show-file-list="false"
            :on-success="uploadSuccess"
            :on-error="uploadError"
            :before-upload="beforeUpload">
        </el-upload>

        <el-form  label-width="80px">
        <!--富文本编辑器组件-->
            <el-form-item label="上传地址" v-if="upLoadType == 1">
                <el-input clearable placeholder="请输入上传地址" v-model="form.url"></el-input>
            </el-form-item>

            <el-form-item label="上传内容" v-if="upLoadType == 2" v-loading="uillUpdateImg">
                <quill-editor
                    v-model="form.content"
                    ref="myQuillEditor"
                    :options="editorOption"
                    @focus="onEditorFocus($event)"
                    @change="onEditorChange($event)">
                </quill-editor>
            </el-form-item>
        </el-form>

        <el-button type="primary" class="subMit" @click="submitBtn" 
            :disabled="!(Boolean(form.title) && Boolean(form.author) && Boolean(form.cover) && Boolean(form.sort))">提交</el-button>
        <!-- <div class="ql-container ql-snow">
            <div class="ql-editor">
                <div v-html="detailContent"></div>
            </div>
        </div> -->
    </div>
</template>
<script>
    import { subBtnFn, getInfoFn } from 'api/game/find/articleAdd';
    import { toolbarOptions } from 'api/game/find/toolbarOptions'; // 工具栏配置
    import { quillEditor } from "vue-quill-editor"; //调用编辑器
    import 'quill/dist/quill.core.css';
    import 'quill/dist/quill.snow.css';
    import 'quill/dist/quill.bubble.css';

    export default {
        components: { quillEditor }, // 注册组件
        data() {
            return {
                uillUpdateImg: false,
                uploadUrl: undefined,
                coverBox: [],
                prefix: {prefix: 'contest'}, // 阿里云额外参数
                upLoadType: 1, // 1为 URL  2为内容
                form: {
                    id: undefined, // 文章id
                    moduleId: undefined, // 模块id
                    title: undefined, // 标题
                    author: undefined, // 作者
                    cover: undefined, // 封面
                    status: 1, // 状态 1:封禁 、2:正常
                    sort: undefined,  // 权重
                    url: undefined,   // 链接地址
                    content: undefined, // 正文内容
                },
                editorOption: {
                    theme: 'snow',  // or 'bubble'
                    modules: {
                        toolbar: {
                            container: toolbarOptions,  // 工具栏
                            handlers: {
                                'image': function (value) {
                                    if (value) {
                                        // 触发input框选择图片文件
                                        document.querySelector('.avatar-uploader input').click()
                                    } else {
                                        this.quill.format('image', false);
                                    }
                                }
                            }
                        }
                    }
                },
            }
        },
        created () {
            this.uploadUrl = this.defaultUploadUrl;
            if (this.$route.query){ // 判断是否有值
                this.form.moduleId = this.$route.query.moduleId;
                if (this.$route.query.id) { // 判断是否为编辑
                    this.form.id = this.$route.query.id;
                    this.getData();
                }
            }
        },
        methods: {
            getData() {
                this.coverBox = []
                getInfoFn({ id: this.form.id }).then(res=>{
                    if (res.code == 0) {
                        this.form = {...res.data};
                        if (res.data.url) { // 1为 URL  2为内容
                            this.upLoadType = 1;
                        } else {
                            this.upLoadType = 2;
                        }
                        this.coverBox.push({url: res.data.cover, name: res.data.cover})
                    } else {
                        this.$message.error( res.message );
                    }
                })
            },

            // 获得焦点事件
            onEditorFocus(e){ this.lenIndex = e.getSelection().index; },
            // 内容改变事件
            onEditorChange(e){ this.lenIndex = e.quill.getSelection().index; },
            escapeStringHTML(str) {  // 转码
                str = str.replace(/</g,'<');
                str = str.replace(/>/g,'>');
                return str;
            },
            /* handleSingleSuccess (res, file) { // res为图片服务器返回的数据
                let quill = this.$refs.myQuillEditor.quill; // 获取富文本组件实例
                if (res.code == '0') {
                    // 插入图片  res.info为服务器返回的图片地址
                    quill.insertEmbed(this.lenIndex, 'image', res.data)
                    // 调整光标到最后
                    quill.setSelection(this.lenIndex + 1)
                } else {
                    this.$message.error('图片插入失败')
                }
                this.quillUpdateImg = false;  // loading动画消失
            }, */


            // 富文本图片上传前
            beforeUpload() { this.quillUpdateImg = true; },
            uploadSuccess(res, file) {  // res为图片服务器返回的数据
                let quill = this.$refs.myQuillEditor.quill;
                if (res.code == 0) {
                    let length = quill.getSelection().index; // 获取光标所在位置
                    quill.insertEmbed(length, 'image', res.data); // 插入图片  res.info为服务器返回的图片地址
                    quill.setSelection(length + 1); // 调整光标到最后
                } else {
                    this.$message.error('图片插入失败')
                }
                this.quillUpdateImg = false; // loading动画消失
            },
            // 富文本图片上传失败
            uploadError() {
                // loading动画消失
                this.quillUpdateImg = false;
                this.$message.error('图片插入失败');
            },
            submitBtn() {
                if (this.upLoadType == 1) { // 1为 URL  2为内容
                    this.form.content = undefined;
                    if (!this.form.url) {
                        this.$message.error({ message: '请填写您要上传的数据!' });
                        return;
                    }
                } else {
                    this.form.url = undefined;
                    if (!this.form.content) {
                        this.$message.error({ message: '请填写您要上传的数据!' });
                        return;
                    }
                }
                subBtnFn({...this.form}).then(res => {
                    if (res.code == 0) {
                        this.$message({ showClose: true, message: '数据添加成功,即将进行跳转', type: 'success' });
                        setTimeout(() => {
                            this.$router.push({
                                path: '/find/articleManage',
                                query:{
                                    copywritingId: this.form.moduleId
                                }
                            });
                        }, 1500);
                    }
                })
            },
            // 封面上传
            handleAvatarSuccess(res, file) { this.form.cover = res.data; },
            handleRemove (file, fileList) { this.edit.cover = ''; },
        },
    }

</script>
<style>
    #articleAdd {
        margin: 30px;
    }

    #articleAdd .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    #articleAdd .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    #articleAdd .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    #articleAdd .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
    #articleAdd .subMit {
        /* float: right; */
        margin: 20px 0;
    }
</style>

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
对于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`方法将图片地址插入到编辑器中。 请注意,这只是一个示例,具体的上图片逻辑可能因你的项目需求而有所不同。你需要根据自己的实际情况进行相应的修改。 希望以上信息能对你有所帮助!如果你还有其他问题,请继续提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值