Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Summernote OnImageUpload not being executed

I just strat using the latest version of summernote. Version 7. For some reason this image upload code is not working. The line onImageUpload: function(files) {sendFile(files[0]);} is not even being executed.

The image successfully appears in the editor but in base64 and it is not uploaded in the uplaods/ folder

All includes have been successfully called.

Js inserted in the head tag of the page

<script>
            $(document).ready(function() {
                $('#summernote').summernote({
                    height: 200,
                    onImageUpload: function(files) {
                        sendFile(files[0]);
                    }
                });
                function sendFile(file) {
                    var data = new FormData();
                    data.append("file", file);//You can append as many data as you want. Check mozilla docs for this
                    $.ajax({
                        data: data,
                        type: "POST",
                        url: 'uploader.php',
                        cache: false,
                        contentType: false,
                        processData: false,
                        success: function(url) {
                            $("#summernote").summernote("insertImage", url);
                        }
                    });
                }
            });
        </script>

uploader.php

$dir_name = "uploads/";
    move_uploaded_file($_FILES['file']['tmp_name'],$dir_name.$_FILES['file']['name']);
    echo $dir_name.$_FILES['file']['name'];

Summernote is really easy to use. I jus need to get the image upload working to have the 1000%nof it.

Please, can anyone help me about this?

like image 969
lucasboko Avatar asked Dec 11 '15 23:12

lucasboko


Video Answer


1 Answers

The position of callbacks in options is changed after v0.7.0

$('#summernote').summernote({
    height: 200,
    callbacks: {
        onImageUpload : function(files, editor, welEditable) {

             for(var i = files.length - 1; i >= 0; i--) {
                     sendFile(files[i], this);
            }
        }
    }
});

and sendFile function

function sendFile(file, el) {
var form_data = new FormData();
form_data.append('file', file);
$.ajax({
    data: form_data,
    type: "POST",
    url: 'uploader.php',
    cache: false,
    contentType: false,
    processData: false,
    success: function(url) {
        $(el).summernote('editor.insertImage', url);
    }
});
}
like image 141
Bheru Lal Lohar Avatar answered Oct 19 '22 03:10

Bheru Lal Lohar