upload image from local into tinyMCE

后端 未结 8 832
小鲜肉
小鲜肉 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:13

    !!!!ENJOY!!! here is the solution to load directly from local computer

    JSFIDDLE DEMO

    Local image loading in to tinymce
    Image size should be lessthan 500kb

    JAVA SCRIPT CODE

    `

    tinymce.init({
        selector: "textarea",
        toolbar: "mybutton",
        setup: function(editor) {
            editor.addButton('mybutton', {
                text:"IMAGE",
                icon: false,
                onclick: function(e) {
                    console.log($(e.target));
                    if($(e.target).prop("tagName") == 'BUTTON'){
                        console.log($(e.target).parent().parent().find('input').attr('id'));
                        if($(e.target).parent().parent().find('input').attr('id') != 'tinymce-uploader') {
                            $(e.target).parent().parent().append('');
                        }
                        $('#tinymce-uploader').trigger('click');
                        $('#tinymce-uploader').change(function(){
                            var input, file, fr, img;
    
                            if (typeof window.FileReader !== 'function') {
                                write("The file API isn't supported on this browser yet.");
                                return;
                            }
    
                            input = document.getElementById('tinymce-uploader');
                            if (!input) {
                                write("Um, couldn't find the imgfile element.");
                            } else if (!input.files) {
                                write("This browser doesn't seem to support the `files` property of file inputs.");
                            } else if (!input.files[0]) {
                                write("Please select a file before clicking 'Load'");
                            } else {
                                file = input.files[0];
                                fr = new FileReader();
                                fr.onload = createImage;
                                fr.readAsDataURL(file);
                            }
    
                            function createImage() {
                                img = new Image();
                                img.src = fr.result;
                                editor.insertContent('');
                            }
                        });
    
                    }
    
                    if($(e.target).prop("tagName") == 'DIV'){
                        if($(e.target).parent().find('input').attr('id') != 'tinymce-uploader') {
                            console.log($(e.target).parent().find('input').attr('id'));                                
                            $(e.target).parent().append('');
                        }
                        $('#tinymce-uploader').trigger('click');
                        $('#tinymce-uploader').change(function(){
                            var input, file, fr, img;
    
                            if (typeof window.FileReader !== 'function') {
                                write("The file API isn't supported on this browser yet.");
                                return;
                            }
    
                            input = document.getElementById('tinymce-uploader');
                            if (!input) {
                                write("Um, couldn't find the imgfile element.");
                            } else if (!input.files) {
                                write("This browser doesn't seem to support the `files` property of file inputs.");
                            } else if (!input.files[0]) {
                                write("Please select a file before clicking 'Load'");
                            } else {
                                file = input.files[0];
                                fr = new FileReader();
                                fr.onload = createImage;
                                fr.readAsDataURL(file);
                            }
    
                            function createImage() {
                                img = new Image();
                                img.src = fr.result;
                                 editor.insertContent('');
                            }
                        });
                    }
    
                    if($(e.target).prop("tagName") == 'I'){
                        console.log($(e.target).parent().parent().parent().find('input').attr('id')); if($(e.target).parent().parent().parent().find('input').attr('id') != 'tinymce-uploader') {               $(e.target).parent().parent().parent().append('');
                                                                                               }
                        $('#tinymce-uploader').trigger('click');
                        $('#tinymce-uploader').change(function(){
                            var input, file, fr, img;
    
                            if (typeof window.FileReader !== 'function') {
                                write("The file API isn't supported on this browser yet.");
                                return;
                            }
    
                            input = document.getElementById('tinymce-uploader');
                            if (!input) {
                                write("Um, couldn't find the imgfile element.");
                            } else if (!input.files) {
                                write("This browser doesn't seem to support the `files` property of file inputs.");
                            } else if (!input.files[0]) {
                                write("Please select a file before clicking 'Load'");
                            } else {
                                file = input.files[0];
                                fr = new FileReader();
                                fr.onload = createImage;
                                fr.readAsDataURL(file);
                            }
    
                            function createImage() {
                                img = new Image();
                                img.src = fr.result;
                                 editor.insertContent('');
                            }
                        });
                    }
    
                }
            });
        }
    });
    

    `

提交回复
热议问题