javascript - Summernote图片上传

标签 javascript image upload summernote

我对编辑器 Summernote 有疑问。我想将图像上传到服务器上的目录中。 我有一些脚本:

<script type="text/javascript">
  $(function () {
    $(\'.summernote\').summernote({
      height: 200
    });
    $(\'.summernote\').summernote({
     height:300,
     onImageUpload: function(files, editor, welEditable) {
      sendFile(files[0],editor,welEditable);
    }
  });

  });
</script>
<script type="text/javascript">
  function sendFile(file, editor, welEditable) {
    data = new FormData();
    data.append("file", file);
    url = "http://localhost/spichlerz/uploads";
    $.ajax({
      data: data,
      type: "POST",
      url: url,
      cache: false,
      contentType: false,
      processData: false,
      success: function (url) {
        editor.insertImage(welEditable, url);
      }
    });
  }
</script>



<td><textarea class="summernote" rows="10" cols="100" name="tekst"></textarea></td>

当然,我有所有的js和CSS文件。 我做错了什么?如果我点击图片上传并转到编辑器,图片不在文本区域中。

如果我删除 sendFile 函数和 onImageUpload:图像保存在 base64 上。

summernote 链接:http://hackerwins.github.io/summernote/

最佳答案

我测试了这段代码并且有效

Javascript

<script>
$(document).ready(function() {
  $('#summernote').summernote({
    height: 200,
    onImageUpload: function(files, editor, welEditable) {
      sendFile(files[0], editor, welEditable);
    }
  });

  function sendFile(file, editor, welEditable) {
    data = new FormData();
    data.append("file", file);
    $.ajax({
      data: data,
      type: "POST",
      url: "Your URL POST (php)",
      cache: false,
      contentType: false,
      processData: false,
      success: function(url) {
        editor.insertImage(welEditable, url);
      }
    });
  }
});
</script>

PHP

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

更新:

在 0.7.0 之后,onImageUpload 应该在 callbacks 选项内,正如@tugberk 所提到的

$('#summernote').summernote({
    height: 200,
    callbacks: {
        onImageUpload: function(files, editor, welEditable) {
            sendFile(files[0], editor, welEditable);
        }
    }
});

关于javascript - Summernote图片上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21628222/

相关文章:

Android 无法通过 Http Post 发送大的 json 字符串

php - 是否需要对 PHP 图像上传进行防病毒检查?

javascript - Uncaught ReferenceError : gsap is not defined

javascript - 使用此特定代码从另一个数组中删除一个数组

c# - 如何在 C# 中检查损坏的 TIFF 图像?

xcode - 在 Swift 中显示/隐藏图像

javascript - 以输入图像类型选择图片并加载到图像标签中

database - 将大视频文件上传到服务器,解决方案?

javascript - 当 angularjs 抛出错误时,如何使 TestCafe 测试失败?

javascript - 使用 API v3 在 Angular 上添加动态标记