Summernote图片上传错误

时间:2015-10-05 11:38:32

标签: php image codeigniter summernote

我使用codeigniter,twitter bootstrap和summernote作为我的WYSIWYG编辑器。我面临着图片上传的一些问题。每当使用Summernote上传图像时,它都会将图像序列化为base64字符串。那个base64字符串很长,以至于它不适合phpmyadmin中的文本数据类型。我想要做的是,使用PHP上传功能上传图像并将其url存储在数据库而不是base64字符串中。我该怎么做?

参考this帖子,这是代码,

$(function() {
  $('.summernote').summernote({
    height: 100,
    onImageUpload: function(files, editor, welEditable) {
            sendFile(files[0], editor, welEditable);
        }
  });
  function sendFile(file, editor, welEditable) {
        data = new FormData();
        data.append("files", file);
        upload_url = "<?php echo base_url(); ?>" + "dashboard/uploader/";
        $.ajax({
            data: data,
            type: "POST",
            url: upload_url,
            cache: false,
            contentType: false,
            processData: false,
            success: function(url) {
                editor.insertImage(welEditable, url);
            }
        });
    }
});

仪表板类中的上传器方法是我的php上传代码所在的位置。这是PHP代码,

public function uploader()
{
    $this->load->helper('file');
    if ($_FILES['files']['name']) {
        if (!$_FILES['files']['error']) {
            $name = md5(rand(100, 200));
            $ext = explode('.', $_FILES['files']['name']);
            $filename = $name . '.' . $ext[1];
            $destination = base_url().'uploads/' . $filename; 
            $location = $_FILES["files"]["tmp_name"];
            move_uploaded_file($location, $destination);
            echo base_url() . $filename; 
        }
        else
        {
          echo  $message = 'Ooops!  Your upload triggered the following error:  '.$_FILES['files']['error'];
        }
    }
}

每当我上传图片时,它都不会在wysiwyg编辑器中显示。我哪里错了?

现在看起来像这样:

$(function() {
  $('.summernote').summernote({
    height: 100,
    onImageUpload: function(files) {
            sendFile(files[0]);
        }
  });
  function sendFile(file) {
        data = new FormData();
        data.append("files", file);
        upload_url = "<?php echo base_url(); ?>" + "dashboard/uploader/";
        $.ajax({
            data: data,
            type: "POST",
            url: upload_url,
            cache: false,
            contentType: false,
            processData: false,
            success: function(url) {
                 $(this).summernote("insertImage", url);
            }
        });
    }
});

1 个答案:

答案 0 :(得分:0)

您使用的是哪个版本的夏令营?最新(0.6.16)summernote调用自定义OnImageUpload函数只有一个参数 - files,没有editorwelEditable

见: https://github.com/summernote/summernote/blob/v0.6.16/src/js/EventHandler.js#L117

请参阅javascript code of django-summernote

Summernote在0.6.5之后改变了处理图像的行为。请参阅changes