Quill editor + VueJS2 image upload: Base64 image to URL

Margus Kevin picture Margus Kevin · May 8, 2017 · Viewed 12.2k times · Source

Using editor from here: https://github.com/surmon-china/vue-quill-editor

I want to save images from Quill editor to MySQL database, but bigger images in base64 are too long to insert.

I tried to write custom image handler, so that it auto uploads image to server and server returns image URL but now I'm stuck.

Here is my current HTML:

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

Adding image handler to editor like this:

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

And my own handler:

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);
    });
},

I tried this: Add support for custom image handler But it doesn't work, since image is always true and callback is undefined.

Tried this too: Quill imageHandler Demo It has same problems first link.

Currently server is hardcoded to return "http://localhost/images/php.jpg"

If possible I would not use any libraries (jQuery, dropzone, etc)

I thought I can perhaps check if image was inserted in onEditorChange(), then send request to server, get URL, search for this base64 in editor and replace it with URL, but it doesn't seem right.

Answer

Alruna L picture Alruna L · May 24, 2017

set handlers in your options like this

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>