从dropzone删除文件缩略图

时间:2016-11-08 05:26:42

标签: php jquery dropzone.js

以下是我用于通过dropzone上传图片的代码。

<script>

  Dropzone.options.uploaddeadlineimages = {

  // The camelized version of the ID of the form element
  // The configuration 
    paramName: 'files',       
    url:"<?=base_url()."Product/upload_listing_images";?>",
    dictDefaultMessage: "<img src='<?=base_url()."public/images/";?>/frontend/camera-black.png'><h2>Drag and drop your photos here to upload</h2><p><a href='javascript:void(0)'>Or Click here to browse for photos</a></p>",   
    uploadMultiple: false,
    createImageThumbnails: true,
    addRemoveLinks: true,
    parallelUploads:100,
    dictInvalidFileType:'Please upload only valid file type(png,jpg,gif,pdf)',
    clickable:true,
    maxFiles:100,   
    autoProcessQueue: true,

    success: function( file, response ) {
        var return_value = response;
        var old_value = $('#listing_images').val();
        if(old_value=="" || old_value==" " || old_value==null){
            var new_value = return_value;
        }else{
            var new_value = old_value+","+return_value;
        }
        $('#listing_images').val(new_value);
    },


  // The setting up of the dropzone

  init: function() {
    var myDropzone = this;
    //alert after success 

    this.on('queuecomplete', function( file, resp ){
        //alert('hahahahahaha');
    });
    // First change the button to actually tell Dropzone to process the queue.

    document.getElementById("create_listing_button").addEventListener("click", function(e) {

      // Make sure that the form isn't actually being sent.


    });
    // Listen to the sendingmultiple event. In this case, it's the sendingmultiple event instead

    // of the sending event because uploadMultiple is set to true.

    this.on("sendingmultiple", function() {
    });

    this.on("successmultiple", function(files, response) {

      // Gets triggered when the files have successfully been sent.

      // Redirect user or notify of success.

    });

    this.on("errormultiple", function(files, response) {

      // Gets triggered when there was an error sending the files.

      // Maybe show form again, and notify user of error

    });

  }
}
  </script>

代码有这个parth,用于将新文件名附加到隐藏字段,以便我可以将这些名称保存在数据库中。但问题是,当我点击删除按钮时,我还需要从隐藏字段中删除该文件的名称。我从服务器获取加密名称。

success: function( file, response ) {
        var return_value = response;
        var old_value = $('#listing_images').val();
        if(old_value=="" || old_value==" " || old_value==null){
            var new_value = return_value;
        }else{
            var new_value = old_value+","+return_value;
        }
        $('#listing_images').val(new_value);
    },

我不需要确切的代码。我只需要一种方法,当我点击删除按钮时,我可以通过该方法将新文件名传递给函数。这不应该阻止删除执行默认功能

1 个答案:

答案 0 :(得分:0)

我找到了答案。只需根据您的需要更新成功部分。但在我的情况下,我需要图像名称作为预览元素的ID。它将以这种方式完成。

success: function( file, response ) {
        var return_value = response;
        var old_value = $('#listing_images').val();
        if(old_value=="" || old_value==" " || old_value==null){
            var new_value = return_value;
             file.previewElement.id = response;
        }else{
            file.previewElement.id = response;
            var new_value = old_value+","+return_value;
        }
        $('#listing_images').val(new_value);    
    },

在删除按钮后更改列表的值只需在dropzone.js文件中添加以下代码(就像我这样做的方式)。 代码从第274行开始。只需从此

更改它
removeFileEvent = (function(_this) {
            return function(e) {
              e.preventDefault();
              e.stopPropagation();
              if (file.status === Dropzone.UPLOADING) {
                return Dropzone.confirm(_this.options.dictCancelUploadConfirmation, function() {
                  return _this.removeFile(file);
                });
              } else {
                if (_this.options.dictRemoveFileConfirmation) {
                  return Dropzone.confirm(_this.options.dictRemoveFileConfirmation, function() {

                  return _this.removeFile(file);

                  });
                } else {

                 return _this.removeFile(file);

                }
              }
            };
          })(this);
          _ref2 = file.previewElement.querySelectorAll("[data-dz-remove]");
          _results = [];
          for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
            removeLink = _ref2[_k];
            _results.push(removeLink.addEventListener("click", removeFileEvent));

          }
          return _results;
        }
      },

到此(只添加了四行。请小心,否则你可能搞得一切)

removeFileEvent = (function(_this) {
            return function(e) {
              e.preventDefault();
              e.stopPropagation();
              if (file.status === Dropzone.UPLOADING) {
                return Dropzone.confirm(_this.options.dictCancelUploadConfirmation, function() {
                  return _this.removeFile(file);
                });
              } else {
                if (_this.options.dictRemoveFileConfirmation) {
                  return Dropzone.confirm(_this.options.dictRemoveFileConfirmation, function() {
                    var id = $(this).closest("div").prop("id");
                    update_img_list(id);
                  return _this.removeFile(file);

                  });
                } else {
                     var id = $(this).closest("div").prop("id");
                    update_img_list(id);
                 return _this.removeFile(file);

                }
              }
            };
          })(this);
          _ref2 = file.previewElement.querySelectorAll("[data-dz-remove]");
          _results = [];
          for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
            removeLink = _ref2[_k];
            _results.push(removeLink.addEventListener("click", removeFileEvent));

          }
          return _results;
        }
      },

在文件更改结束时添加此函数('#listing_images')到('#id_of_your_field_which_contains_the_list')

function update_img_list(id){

  var list = $('#listing_images').val();
  separator = ",";
  var values = list.split(separator);
  for(var i = 0 ; i < values.length ; i++) {
    if(values[i] == id) {
      values.splice(i, 1);
      $('#listing_images').val(values.join(separator)) ;
    }
  }
  return list;

}