2015-10-05 143 views
1

我使用codeigniter,twitter bootstrap和summernote作为我的所见即所得编辑器。我面临图像上传的一些问题。使用Summernote上传图像时,它会以base64字符串序列化图像。该base64字符串太长,以至于它不适合phpmyadmin中的文本数据类型。我想要做的是,使用PHP上传函数上传图像,并将其url存储在数据库中而不是base64字符串。我会怎么做?Summernote图像上传错误

参考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']; 
     } 
    } 
} 

每当我上传的图片,它不表现出来的所见即所得的编辑器。我哪里错了?

现在看起来是这样的:

$(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); 
      } 
     }); 
    } 
}); 
+0

http://stackoverflow.com/a/35815418/2847436,这可以帮助你。 –

回答

0

哪个summernote的版本您使用的?最新的(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

+0

即时通讯使用v0.6.13 – cyberrspiritt

+0

所以我如何解决它与使用编辑器和welEditable? – cyberrspiritt

+0

无需使用编辑器和welEditable。就像'$(this).summernote(“insertImage”,url);'请参考https://github.com/summernote/django-summernote/blob/master/django_summernote/templates/django_summernote/widget_inplace.html#L45- L66 – lqez