【问题标题】:How to delete uploaded image from server when working using Summernote Editor使用 Summernote 编辑器时如何从服务器中删除上传的图像
【发布时间】:2017-08-28 10:33:14
【问题描述】:

我正在使用带有图像上传功能的 Summernote 编辑器,但我注意到,当我在编辑器中添加图像内容时,它会成功上传到我的服务器文件夹中,但尚未保存,但是当我在编辑器中删除图像时,它不是从我的服务器文件夹中删除。

【问题讨论】:

    标签: php jquery html


    【解决方案1】:

    要从服务器删除文件,需要使用onMediaDelete,但使用方法因summernote版本不同而不同,有时在文档中很难找到

    对于 SUMMERNOTE 0.6.x

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

    SUMMERNOTE 0.7.x

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

    对于 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);
                }
            }
        });
    });
    

    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);
            }
        });
    }
    

    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';
            }
    ?>
    

    图片上传参考 - Summernote v0.8 Image upload to Server

    【讨论】:

      【解决方案2】:

      您可以在此处使用此代码

      <?php
      
      // include Database connection file
      require_once("../../resources/directories.inc.php");
      
      // Return the domain uri
      function url(){
        return sprintf(
          "%s://%s",
          isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http',
          $_SERVER['SERVER_NAME'],
          $_SERVER['REQUEST_URI']
        );
      }
      
      // Upload file into directory
      if (isset($_FILES['file']['name']) && isset($_POST['upload_directory']) && isset($_POST['action']) && $_POST['action']=='upload_file') {
          if (!$_FILES['file']['error']) {
              $folder_dir = $_POST['upload_directory'];
              $name = rand(100,1000).'_'.date('Ymd');
      
              $ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
              $filename = $name.'.'.$ext;
              $destination = '../../uploads/'.$folder_dir.'/'.$filename; //change this directory
              $location = $_FILES["file"]["tmp_name"];
              move_uploaded_file($location, $destination);
              echo BASE_URL.'uploads/'.$folder_dir.'/'.$filename;
          } else {
              echo 'Ooops!  Your upload triggered the following error:  '.$_FILES['file']['error'];
          }
      }
      
      // Delete file from directory
      if (isset($_POST['img_src']) && isset($_POST['delete_directory']) && isset($_POST['action']) && $_POST['action']=='remove_file') {
          $full_img_src = $_POST['img_src'];
          $folder_dir = $_POST['delete_directory'];
      
          $upload_dir_url = url().BASE_URL.'uploads/'.$folder_dir.'/';
          $file_name = str_replace($upload_dir_url, '', $full_img_src); // striping url to get file name
          $upload_path = '../../uploads/'.$folder_dir.'/'.$file_name; // path to delete from
      
          if(unlink($upload_path)){
              echo 'File removed successfully';
          } else {
              echo 'File could not be removed successfully';
          }
      }
      

      ?>

      Javascript

          const lib_url = '<?php echo BASE_URL."resources/library/summernote.lib.php"; ?>';
      $('.summernote_editor').summernote({
          tabsize: 2,
          height: 400,
          spellCheck: true,
          toolbar: [
            ['style', ['style']],
            ['font', ['bold', 'underline', 'italic', 'superscript', 'subscript', 'clear']],
            ['fontname', ['fontname','fontsize']],
            ['color', ['color']],
            ['para', ['ul', 'ol', 'paragraph']],
            ['table', ['table']],
            ['insert', ['link', 'picture', 'video']],
            ['view', ['fullscreen', 'help', 'undo', 'redo']],
          ],
          callbacks: {
              onImageUpload: function(files, editor, welEditable) {
                              sendFile(files[0], editor, welEditable);
                          },
                          onMediaDelete : function(target) { 
                              deleteFile(target[0].src);
                          }
          }
      });
      
      function sendFile(file, editor, welEditable) {
          data = new FormData();
          data.append("file", file);
          data.append("action", 'upload_file');
          data.append("upload_directory", 'news');
          $.ajax({
              data: data,
              type: "POST",
              url: lib_url,
              cache: false,
              processData: false,
              contentType: false,
              success: function(url) {
                  var image = $('<img>').attr('src', url);
                  $('.summernote_editor').summernote("insertNode", image[0]);
              }
          });
      }
      
      function deleteFile(src) {
          data = new FormData();
          data.append("img_src", src);
          data.append("action", 'remove_file');
          data.append("delete_directory", 'news');
          $.ajax({
              data: data,
              type: "POST",
              url: lib_url,
              cache: false,
              processData: false,
              contentType: false,
              success: function(resp) {
                  console.log(resp);
              }
          });
      }
      

      【讨论】:

        猜你喜欢
        • 2015-03-20
        • 1970-01-01
        • 2016-11-14
        • 2017-12-07
        • 1970-01-01
        • 1970-01-01
        • 2019-02-11
        • 1970-01-01
        • 2019-12-21
        相关资源
        最近更新 更多