Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add removefile option in dropzone plugin?

I have used dropzone.js (http://www.dropzonejs.com/). I am trying to add delete button to delete the image. but i am getting javascript undefined error

 <script>
  Dropzone.options.myDropzone = {
init: function() {
 addRemoveLinks: true,  
thisDropzone = this;
 $.get('photo-add.php', function(data) {
 $.each(data, function(key,value){
var mockFile = { name: value.name, size: value.size };
  thisDropzone.options.addedfile.call(thisDropzone, mockFile);
 thisDropzone.options.thumbnail.call(thisDropzone, mockFile,      "upload/"+value.name);

});
 });

 this.on("removedfile", function(file) {
alert(file.name);
console.log(file);
     // Create the remove button
  var removeButton = Dropzone.createElement("<button>Remove file</button>");

 /    / Capture the Dropzone instance as closure.
  var _this = this;

 // Listen to the click event
 removeButton.addEventListener();

   // Add the button to the file preview element.
  file.previewElement.appendChild(removeButton);
    });
  }
  };
     </script>
like image 295
markatte team Avatar asked Nov 16 '15 05:11

markatte team


2 Answers

I think you are not configuring the dropzone in proper way.

init: function() {
 addRemoveLinks: true, 

The code above is not valid .

Use like this

Dropzone.options.myAwesomeDropzone = {
  addRemoveLinks: true,
  init: function() {

  }

  ...

};

or else you can also use like this.addRemoveLinks = true

If you want to handle the event of removing file you can use like this

removedfile: function(file) {
    x = confirm('Do you want to delete?');
    if(!x)  return false;
}

Handle the deletion of file on server side.

on success method of dropzone push the file name to an array eg : file_up_names=[];

 success:function(file){
   file_up_names.push(file.name);

now on delete get the name and pass it to the php page for file deletion.

 removedfile: function(file) {

    x = confirm('Do you want to delete?');
    if(!x)  return false;
    for(var i=0;i<file_up_names.length;++i){

      if(file_up_names[i]==file.name) {

        $.post('delete_file.php', 
             {file_name:file_up_names[i]},
           function(data,status){
             alert('file deleted');
             });
       }
    }
 }

also note if you have changed the file name on server side you have to get back that file name to delete.

like image 196
Tintu C Raju Avatar answered Oct 21 '22 01:10

Tintu C Raju


Try below code:

Dropzone.autoDiscover = false;
var acceptedFileTypes = "image/*"; //dropzone requires this param be a comma separated list
var fileList = new Array;
var i = 0;
$("#dropzone").dropzone({
    addRemoveLinks: true,
    maxFiles: 10, //change limit as per your requirements
    dictMaxFilesExceeded: "Maximum upload limit reached",
    acceptedFiles: acceptedFileTypes,
    dictInvalidFileType: "upload only JPG/PNG",
    init: function () {

        // Hack: Add the dropzone class to the element
        $(this.element).addClass("dropzone");

        this.on("success", function (file, serverFileName) {
            fileList[i] = {
                "serverFileName": serverFileName,
                "fileName": file.name,
                "fileId": i
            };
            $('.dz-message').show();
            i += 1;
        });
        this.on("removedfile", function (file) {
            var rmvFile = "";
            for (var f = 0; f < fileList.length; f++) {
                if (fileList[f].fileName == file.name) {
                    rmvFile = fileList[f].serverFileName;
                }
            }

            if (rmvFile) {
                $.ajax({
                    url: path, //your php file path to remove specified image
                    type: "POST",
                    data: {
                        filenamenew: rmvFile,
                        type: 'delete',
                    },
                });
            }
        });

    }
});
like image 35
Dhaval Bharadva Avatar answered Oct 21 '22 02:10

Dhaval Bharadva