Summernote 图片上传不工作

Summernote image upload not working

我遇到 summernote 图片上传 的问题。

我的脚本看起来像这样:

<script>
 $(document).ready(function() {

var IMAGE_PATH = 'http://localhost/dist/images/';

$('.summernote').summernote({
    height: 300,
    callbacks : {
        onImageUpload: function(image) {
            uploadImage(image[0]);
        }
    }
});

function uploadImage(image) {
    var data = new FormData();
    data.append("image",image);
    $.ajax ({
        data: data,
        type: "POST",
        url: "uploader.php",
        cache: false,
        contentType: false,
        processData: false,
        success: function(url) {
            var image = IMAGE_PATH + url;
            $('.summernote').summernote('insertImage', image);
            },
            error: function(data) {
                console.log(data);
                }
        });
    }

});
</script>

和uploader.php有以下代码:

<?php
$image = $_FILES['image']['name'];
$uploaddir = 'images/';
$uploadfile = $uploaddir . basename($image);
if( move_uploaded_file($_FILES['image']['tmp_name'],$uploadfile)) {
    echo $uploadfile;
} else {
    echo "Unable to Upload";
}
?>

图像文件正在成功上传到目录 'images'。

但是 $('.summernote').summernote('insertImage', image);不会将上传的图片 Link 附加到编辑器。

我错过了什么?是的,我尝试提醒 var 'image' 并且它具有所需的值。

你可以试试 :var IMAGE_PATH = 'http://localhost/dist/'; 因为你的 php 代码:$uploadfile=>'images/xxx.jpg',html url 使用了两次图片,比如 ../images/images/xxx.jpg。所以 summernote 图片上传不起作用! 谢谢!

这是我的代码

  <script>
 $(document).ready(function() {

var IMAGE_PATH = 'http://localhost:81/summernote-develop/examples/';

$('.summernote').summernote({
    height: 300,
    callbacks : {
        onImageUpload: function(image) {
            uploadImage(image[0]);
        }
    }
});

function uploadImage(image) {
    var data = new FormData();
    data.append("image",image);
    $.ajax ({
        data: data,
        type: "POST",
        url: "uploader.php",
        cache: false,
        contentType: false,
        processData: false,
        success: function(url) {
            var image = IMAGE_PATH + url;
            $('.summernote').summernote('insertImage', image);
   //console.log(image);
            },
            error: function(data) {
                console.log(data);
                }
        });
    }

});
</script>

和你一样的php码

和代码 dir:D:\WWW\summernote-develop\examples\ 图片 dir:D:\WWW\summernote-develop\examples\图片

使用下面的代码。它应该完美运行

PHP 脚本

<?php

// include Database connection file
require_once("../../resources/directories.inc.php");

if (isset($_FILES['file']['name'])) {
    if (!$_FILES['file']['error']) {
        $name = rand(100,1000).'_'.date('Ymd');
        $ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
        $filename = $name.'.'.$ext;
        $destination = '../../uploads/news/'.$filename; //change this directory
        $location = $_FILES["file"]["tmp_name"];
        move_uploaded_file($location, $destination);
        echo BASE_URL.'uploads/news/'.$filename;
    } else {
        echo 'Ooops!  Your upload triggered the following error:  '.$_FILES['file']['error'];
    }
}

?>

JS 代码

$('.summernote_editor').summernote({
    tabsize: 2,
    height: 400,
    spellCheck: true,
    toolbar: [
      ['style', ['style']],
      ['font', ['bold', 'underline', 'italic', 'superscript', 'subscript', 'clear']],
      ['fontname', ['fontname','fontsize']],
      ['color', ['color']],
      ['para', ['ul', 'ol', 'paragraph']],
      ['table', ['table']],
      ['insert', ['link', 'picture', 'video']],
      ['view', ['fullscreen', 'help', 'undo', 'redo']],
    ],
    callbacks: {
        onImageUpload: function(files, editor, welEditable) {
            sendFile(files[0], editor, welEditable);
        }
    }
});

function sendFile(file, editor, welEditable) {
    var lib_url = '<?php echo BASE_URL."resources/library/upload_summernote.lib.php"; ?>';
    data = new FormData();
    data.append("file", file);
    $.ajax({
        data: data,
        type: "POST",
        url: lib_url,
        cache: false,
        processData: false,
        contentType: false,
        success: function(url) {
            var image = $('<img>').attr('src', url);
            $('.summernote_editor').summernote("insertNode", image[0]);
        }
    });
}