Summernote image upload

后端 未结 5 1500
花落未央
花落未央 2020-11-27 09:51

I have a problem with editor Summernote. I want to upload images into a catalog on the server. I have some script:



        
5条回答
  •  隐瞒了意图╮
    2020-11-27 10:15

    This code worked well with new version (v0.8.12) (2019-05-21)

    $('#summernote').summernote({
            callbacks: {
                onImageUpload: function(files) {
                    for(let i=0; i < files.length; i++) {
                        $.upload(files[i]);
                    }
                }
            },
            height: 500,
        });
    
        $.upload = function (file) {
            let out = new FormData();
            out.append('file', file, file.name);
    
            $.ajax({
                method: 'POST',
                url: 'upload.php',
                contentType: false,
                cache: false,
                processData: false,
                data: out,
                success: function (img) {
                    $('#summernote').summernote('insertImage', img);
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    console.error(textStatus + " " + errorThrown);
                }
            });
        };
    

    PHP Code (upload.php)

    if ($_FILES['file']['name']) {
     if (!$_FILES['file']['error']) {
        $name = md5(rand(100, 200));
        $ext = explode('.', $_FILES['file']['name']);
        $filename = $name . '.' . $ext[1];
        $destination = 'images/' . $filename; //change this directory
        $location = $_FILES["file"]["tmp_name"];
        move_uploaded_file($location, $destination);
        echo 'images/' . $filename;//change this URL
     }
     else
     {
      echo  $message = 'Ooops!  Your upload triggered the following error:  '.$_FILES['file']['error'];
     }
    }
    

提交回复
热议问题