O
O
Oleg Kamzin2018-04-30 18:57:12
PHP
Oleg Kamzin, 2018-04-30 18:57:12

How to upload images to server in Summernote editor?

Failed to upload image to server. Found solutions on the internet but they don't work for me.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Ainur Valiev, 2018-05-01
@vaajnur

spoiler
$(document).ready(function() {
        $('#summernote').summernote({
  callbacks: {
    onImageUpload: function(files, editor, $editable) {
        	    sendFile(files[0],editor,$editable);
        	}
  }
});



sendFile = function(file, editor, welEditable) {
    data = new FormData();
    data.append("file", file);

    $.ajax({
        url: '/summernote.php',
        data: data,
        cache: false,
        contentType: false,
        processData: false,
        type: 'POST',
        success: function(data){
            console.log(data);
            $('#description').summernote("editor.insertImage", data, 'filename');
        },
        error: function(jqXHR, textStatus, errorThrown) {
            console.log(textStatus+" "+errorThrown);
        }
    });
}


    });

and handler
if( isset($_FILES['file']) && $_FILES['file']['error'] == 0 ) {
        if( move_uploaded_file( $_FILES['file']['tmp_name'], $_FILES['file']['name'] ) ) 
            	echo 'good';
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question