【问题标题】:Quill editor + VueJS2 image upload: Base64 image to URLQuill 编辑器 + VueJS2 图片上传:Base64 图片到 URL
【发布时间】:2017-10-07 00:43:30
【问题描述】:

从这里使用编辑器:https://github.com/surmon-china/vue-quill-editor

我想将图像从 Quill 编辑器保存到 MySQL 数据库,但是 base64 中较大的图像太长而无法插入。

我尝试编写自定义图像处理程序,以便它自动将图像上传到服务器并且服务器返回图像 URL,但现在我被卡住了。

这是我当前的 HTML:

<quill-editor v-model="content"
    :options="editorOption"
    @onEditorBlur($event)"
    @onEditorFocus($event)"
    @onEditorReady($event)"
    @onEditorChange($event)">
</quill-editor>

像这样向编辑器添加图像处理程序:

onEditorReady(editor) {
    editor.getModule('toolbar').addHandler('image', this.imageHandler);
    console.log('editor ready!', editor);
},

还有我自己的处理程序:

imageHandler(image, callback){
    console.log(image); // Always true
    console.log(callback); // Always undefined

    // Should get image in here somehow..
    var sendData = {
        image: 'SomethingShouldBeInHere',
    };

    // Send image to server, 
    //  Server will return link to image
    axios.put('/testImageUpload', sendData)
    .then(function(cbData) {
        // In here should add image tag to editor somehow..

    })
    .catch(function (error) {
        console.log(error);
    });
},

我试过这个:Add support for custom image handler 但它不起作用,因为 image 总是 true 并且回调是未定义的。

也试过这个:Quill imageHandler Demo 第一个链接也有同样的问题。

当前服务器被硬编码为返回“http://localhost/images/php.jpg

如果可能,我不会使用任何库(jQuery、dropzone 等)

我想我也许可以检查一下图像是否插入到 onEditorChange() 中,然后向服务器发送请求,获取 URL,在编辑器中搜索这个 base64 并将其替换为 URL,但它似乎不正确。

【问题讨论】:

    标签: javascript image vuejs2 quill


    【解决方案1】:

    像这样在您的选项中设置处理程序

    editorOption: {
      modules: {
       toolbar: {
        container: [['image'], ...],
        handlers: {
         'image': function(){
          document.getElementById('getFile').click()
         }
        }
       } 
      }
    }
    
    
    methods: {
      uploadFunction(e){
      
        //you can get images data in e.target.files
        //an single example for using formData to post to server
        
        
        var form = new FormData()
        form.append('file[]', e.target.files[0])
        
        //do your post
        
        
      }
    }
    <template>
      <quill-editor v-model="content"
                :options="editorOption"
                @change="oneEditorChange($event)">
      </quill-editor>
      <input type="file" id="getFile" @change="uploadFunction($event)" />
    </template>

    【讨论】:

    • 你能提供更多的上下文吗?也许在哪里设置这些处理程序?
    • 搞定了,谢谢!从没想过要添加另一个输入并使用它=)
    • 请您指导我如何从这个编辑器中删除图像和视频上传,我正在我的项目中实现它,我只需要基本的文本编辑功能。谢谢
    • @Benjiro 你可以设置一个自定义工具栏,只包含你想要的功能,只覆盖默认设置。 quilljs.com/docs/modules/toolbar
    【解决方案2】:

    这是我的源代码....

    //Template
    <input type="file" @change="uploadFunction" id="file" hidden>
    
    <quill-editor 
          v-model="model" 
          :options="editorOption" 
          ref="quillEdit">
    </quill-editor>
    

    和脚本

     //script
        import "quill/dist/quill.core.css";
        import "quill/dist/quill.snow.css";
        import "quill/dist/quill.bubble.css";
        import Quill from "quill";
        import { quillEditor } from "vue-quill-editor";
        import ImageResize from "quill-image-resize-module";
        import axios from '~/plugins/axios'
    
        export default {
          data() {
            model: '',
            selectedFile : '',
            editorOption: {
                // some quill options
                modules: {
                  toolbar: {
                    container: [["bold", "image"]],
                    handlers: {
                      image: function() {
                        document.getElementById('file').click()
                      }
                    }
                  },
                  imageResize: {
                    modules: ["Resize", "DisplaySize", "Toolbar"]
                  }
                }
              },
           },
           methods: {
            uploadFunction(e){
                 this.selectedFile = e.target.files[0];
    
              var form = new FormData();
              form.append("file", this.selectedFile);
              form.append("name", this.selectedFile.name);
    
                //upload image to server
                axios.post('media-save', form,{
                 'headers': {
                     'Content-Type': "multipart/form-data"
                  }
                 })
                .then(r => {
                  console.log('success')
    
                  //this code to set your position cursor
    const range = this.$refs.quillEdit.quill.getSelection()
    //this code to set image on your server to quill editor
                  this.$refs.quillEdit.quill.insertEmbed(range.index , 'image', `http://your.api/${r}`)
                })
                .catch(e => {
                  console.log('error')
             }
           }
        }
    

    【讨论】:

    • insertEmbed(range, ... 范围是一个对象。所以应该是range.index
    • @Shirker 谢谢指正,但现在我意识到,如果数据是一个对象,那么图像将放在最后一段,这样我以前就没有被打扰过......:D
    • 我还听说在getSelection() 之前调用..quill.focus() 更安全,在我的情况下this.$refs.quillEdit.quill 将是this.$refs.quillEdit[0].quill,因为$refs 返回数组
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 2015-08-26
    • 2018-01-01
    • 1970-01-01
    • 2015-12-07
    • 2013-01-31
    • 2013-11-03
    相关资源
    最近更新 更多