summernote vue html编辑器不会将图像上传到服务器

时间:2016-07-12 10:23:53

标签: php text editor summernote

我遇到了夏令营图片上传的问题。据我所知,summernote将图像转换为base64,一旦我提交,它就会将图像直接保存在后端数据库服务器上。我有一些代码将图像上传到服务器文件夹..但它对我不起作用...帮帮我朋友。

我的脚本是:

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

          }

        });

       function sendFile(file,editor,welEditable) {
          data = new FormData();
          data.append("file", files);
           $.ajax({
           url: "uploader.php",
           data: data,
           cache: false,
           contentType: false,
           processData: false,
           type: 'POST',
           success: function(data){
           alert(data);
            $('.summernote').summernote("insertImage", data, 'filename');
        },
           error: function(jqXHR, textStatus, errorThrown) {
           console.log(textStatus+" "+errorThrown);
          }
        });
       }
});

Uploader.php

<?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/content/' . $filename; //change this directory
                $location = $_FILES["file"]["tmp_name"];
                move_uploaded_file($location, $destination);
                echo 'http://localhost/ . $filename;//change this URL
            }
            else
            {
              echo  $message = 'Your upload triggered the following error:  '.$_FILES['file']['error'];
            }
        }

        ?>

1 个答案:

答案 0 :(得分:0)

最后我解决了我的错误。只需在我的脚本中插入一个回调函数,它就能正常工作..

preg_match_all('/([^ ]*("[^"]*")[\s])|([^ ]+[\s])/', $string, $matches);