upload image from local into tinyMCE

后端 未结 8 831
小鲜肉
小鲜肉 2021-02-04 02:33

tinyMCE has an insert image button, but how to handle its functionality pls give some code

8条回答
  •  渐次进展
    2021-02-04 03:19

    I know it is an old question. However, I think this answer may help somebody who wants to upload multiple images by using tinyMCE 5.xx. Based on @Chris Lear's and @stephen.hanson's answer, I modify some code to support multiple images uploading. Here is my code. Hope it could help somebody.

    tinymce.init({
    toolbar: 'imageupload',
    setup: function(editor) {
      initImageUpload(editor);
      }
    });
    
    function initImageUpload(editor) {
    // create input and insert in the DOM
     var inp = $(``);
      $(editor.getElement()).parent().append(inp);
    // add the image upload button to the editor toolbar
      editor.addButton('imageupload', {
      text:'IMAGE',
      onAction: function(_) { 
     // when toolbar button is clicked, open file select modal
        inp.trigger('click');
      }
    });
     // when a file is selected, upload it to the server
    inp.on('change',function(){
        for(let i=0;i {
              if (res.status = 200) { 
                  editor.insertContent('');
                 // clear data to avoid uploading same data not working in the second time
                 inp.val('');
              }
          })
      }
    

提交回复
热议问题