【问题标题】:Summernote upload not workingSummernote 上传不工作
【发布时间】:2018-09-17 12:25:57
【问题描述】:

我的 onImageUpload 在夏季注意 jquery 插件不工作。 用于将图像上传到某个文件夹的不同位置,然后是summernote默认位置 我该怎么办?!

index.php

<textarea id="summernote" name="contents"></textarea>

<script>
    $('#summernote').summernote({
        tabsize: 2,
        height: 200,
        focus: true,
        callbacks: {
            onChange: function(contents, $editable) {
                console.log('onChange:', contents, $editable);
            }
        }
    });

    $('#summernote').on('summernote.image.upload', function(we, contents, $editable) {
        data = new FormData ();

        data.append("file",file);

        $.ajax({
            url: "summernote_upload.php",
            type: "POST",
            data: data,
            cache: false,
            contentType: false,
            processData: false,
        });

        $summernote.summernote('insertNode', imgNode);
    });
</script>

summernote_upload.php

require_once ('./../../lib/curl_setup.php'); // curl json&token

$data_array = array(
    "request" => array(
        "image" => "this upload file/base64"
    ),
);
$make_call = callAPI('POST', '/api/v2/admin/products/images', json_encode($data_array));

$response = json_decode($make_call, true);

//$errors   = $response['response']['errors'];
//$data     = $response['response']['data'][0];

print_r($response);exit;

但是,summernote_upload.php 运行良好。 回应是:

{
    "image": {
        "shop_no": 1,
        "path": "/web/upload/NNEditor/20180408/b69c819e36b4abc3f393b731829ab747.gif"
    }
}

我需要解决index.php

【问题讨论】:

  • “不工作”是什么意思?你能扩展一下吗? JS函数调试过吗?

标签: javascript php jquery upload summernote


【解决方案1】:

首先你必须捕获上传图片事件 并且不允许summernote自动添加它

        callbacks: {
               onImageUpload: function(files, editor, $editable) {
                   console.log('onImageUpload');
                   sendFile(files[0],editor,$editable);
        }},

那么您必须自己处理带有 json 的图像上传。 有一个功能:

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

在服务器端你可以保存文件

$allowed = array('png', 'jpg', 'gif','jpeg');
if(isset($_FILES['file']) && $_FILES['file']['error'] == 0){
    $extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
    if(!in_array(strtolower($extension), $allowed)){
        echo '{"status":"error"}';
        exit;
    }
    $newFileAddress = '../files/-'.rand(1000).'.'.$extension;
    if(move_uploaded_file($_FILES['file']['tmp_name'],$newFileAddress)){
     echo $newFileAddress;
     exit;
    }
}

通过返回服务器上的文件路径,您可以轻松地将其插入到夏季笔记中

【讨论】:

    猜你喜欢
    • 2017-01-01
    • 2016-08-15
    • 2015-05-10
    • 2015-07-27
    • 2017-08-21
    • 2014-03-04
    • 2020-03-18
    • 2016-06-13
    • 2015-01-03
    相关资源
    最近更新 更多