Summernote-从服务器删除图像

Mat*_*ins 1 html php jquery summernote

嗨,我已经使用以下链接中的代码来允许将图像上传到服务器。 Summernote图片上传

如果用户从编辑器中删除图像,是否可以实现类似于从服务器中删除图像的操作?如果是这样,我该如何实现呢?

4Je*_*ean 5

要删除文件格式服务器,您需要使用,onMediaDelete但是用法因不同的summernote版本而异,有时很难在文档中找到。

夏令时0.6.x

$(document).ready(function() {
    $('.summernote').summernote({
        height: "300px",
         onMediaDelete : function($target, editor, $editable) {
         alert($target.context.dataset.filename);         
         $target.remove();
    }
    });
});
Run Code Online (Sandbox Code Playgroud)

夏令时0.7.x

$(document).ready(function() {
    $('.summernote').summernote({
        height: "300px",
        onMediaDelete : function(target) {
                deleteFile(target[0].src);
            }

    });
});
Run Code Online (Sandbox Code Playgroud)

FOR SUMMERNOTE 0.8.x(使用回调)

$(document).ready(function() {
    $('#summernote').summernote({
        height: "300px",
        callbacks: {
            onImageUpload: function(files) {
                uploadFile(files[0]);
            },

            onMediaDelete : function(target) {
                // alert(target[0].src) 
                deleteFile(target[0].src);
            }
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

Javascript:使用img src删除文件的示例

function deleteFile(src) {

    $.ajax({
        data: {src : src},
        type: "POST",
        url: base_url+"dropzone/delete_file", // replace with your url
        cache: false,
        success: function(resp) {
            console.log(resp);
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

PHP:删除图像后检索img src的示例

<?php
  $src = $this->input->post('src'); // $src = $_POST['src'];
  $file_name = str_replace(base_url(), '', $src); // striping host to get relative path
        if(unlink($file_name))
        {
            echo 'File Delete Successfully';
        }
?>
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

这就是我所使用的,仅此而已,delete事件不会在像backspace这样的keypress上触发,也许将来keyEvent会处理该事件。