Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear dropzone.js thumbnail image after uploading an image

I have using dropzone.js in mvc by following this tutorial for uploading images but after uploading the image the thumbnail image still showing and I need it to be removed after every time I upload an image.

I have tried to replace the generated HTML after uploading the image using jQuery but it not showing correctly as the first time I need to refresh the page to show correctly but I don't want to do that.

 $("#ImageUpload").replaceWith('<form action="/Products/SaveUploadedFile" method="post" enctype="multipart/form-data" class="dropzone dz-clickable" id="dropzoneForm">'
 +'<div class="fallback">'
 +'<input name="file" type="file" multiple />'
 +'<input type="submit" value="Upload" />'
 +'</div>');
like image 649
Fadi Avatar asked Jul 16 '14 22:07

Fadi


7 Answers

You can try this:

myDropzone.on("complete", function(file) {
  myDropzone.removeFile(file);
});

More information here: http://www.dropzonejs.com/#dropzone-methods

like image 141
Giraldi Avatar answered Nov 19 '22 14:11

Giraldi


removeAllFiles() and removeFile() will trigger the server-side removal too, if you hooked Dropzone to remove files as well.

The solution to clear it only client-side, remove the file preview, and if you had a blank state message, remove the dz-started class to prevent the Dropzone CSS from hiding it:

$('.dropzone')[0].dropzone.files.forEach(function(file) { 
  file.previewElement.remove(); 
});

$('.dropzone').removeClass('dz-started');
like image 22
Flavio Copes Avatar answered Nov 19 '22 13:11

Flavio Copes


Another option (similar to answer of Giraldi, but then when all files are completed):

myDropzone.on("queuecomplete", function () {
    this.removeAllFiles();
});
like image 8
Allie Avatar answered Nov 19 '22 12:11

Allie


it was actually an easy thing to do but some time it go hard so i just delete the dropzone generated code using jquery and add a button with 'id = btnCreate' then

 $('#btnCreate').click(function () {
        $('div.dz-success').remove();
   });

and when i upload the thumbnail image removed after i click the button

like image 5
Fadi Avatar answered Nov 19 '22 13:11

Fadi


Just an fyi...

The method removeAllFiles is not necessarily the prime choice. Which is the same as removeFile(file).

I have an event handler for dropZone's removedfile event... I'm using it to send a server message to delete the respective file from the server (should a user delete the thumbnail after it's been uploaded). Using the method removeAllFiles (as well as the individualized removeFile(file)) fires the event removedfile which deletes the uploaded images in addition to clearing the thumbnails.

So one could add some finessing around this but in the reality of it the method as mentioned in the primary answer on this thread is not correct.

Looking through the api for Dropzone I am not seeing an API call to simply reset or clear the thumbnails... The method disable() will clear the stored file names and what not but does not clear the thumbnails... Seems dropzoneJS is actually missing a critical API call to be honest.

My work around is to manually reset the containing div for dropzone:

document.getElementById("divNameWhereDropzoneClassIs").innerHTML = ""

This clears the thumbnails without firing off the event removedfile which is supposed to be used for deleting an image from the server...

like image 3
kkthxby3 Avatar answered Nov 19 '22 12:11

kkthxby3


docsDropzone.on('complete', function (response)
{
   $(".dz-preview").remove();
});

Above works for me

like image 2
Irfan Ashraf Avatar answered Nov 19 '22 14:11

Irfan Ashraf


try this on your library dropzone dropzone.js; but set time out to auto close 2500

success: function(file) {
  if (file.previewElement) {
    return file.previewElement.classList.add("dz-success"),
    $(function(){
      setTimeout(function(){
        $('.dz-success').fadeOut('slow');
      },2500);
    });
  }
},
like image 1
heriipurnama Avatar answered Nov 19 '22 14:11

heriipurnama