Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dropzone.js .removeAllFiles() does not remove mock files

Tags:

dropzone.js

I noticed that when calling .removeAllFiles() on a dropzone instance that received "mock" files using the technique as shown here does not actually have the desired effect (the mock file is still present).

like image 262
Pierlo Upitup Avatar asked Apr 29 '14 15:04

Pierlo Upitup


People also ask

How do I delete files in Dropzone?

If you want to remove an added file from the dropzone, you can call . removeFile(file) . This method also triggers the removedfile event.

How can I show you the files already stored on server using dropzone JS?

Set Dropzone autoDiscover to false and initialize dropzone on class="dropzone" . Send AJAX POST request from init function to get all the files list. On successfully callback loop on the response and assign { name: value.name, size: value. size } in mockFile .

How do I delete a dropzone after uploading?

Explicitly initialize using dropzone() method and for enabling remove file add addRemoveLinks: true and removefile options. Send AJAX request from the removedfile function where get the file name using the file.name and pass in the AJAX as data. var _ref; return (_ref = file. previewElement) !=

How does dropzone JS work?

Dropzone. js is 'a light weight JavaScript library that turns an HTML element into a "dropzone"'. Users can drag and drop a file onto an area of the page, uploading to a server. If you would like to read more how all of this works skim through the Dropzone.


1 Answers

I also had the same problem. I thinks it happens because the files which are added from the server does not go into the files array and when you call removeAllFiles() it won't work because it is not in the files array atall.
So the obvious solution would be to add them into the files array,it would look something like this

$(function() {
    var mockFile = { name: "banner2.jpg", size: 12345 };
    var myDropzone = new Dropzone("#my-awesome-dropzone");
    myDropzone.options.addedfile.call(myDropzone, mockFile);
    myDropzone.files.push(mockFile); // here you add them into the files array
    myDropzone.options.thumbnail.call(myDropzone, mockFile,
        "http://localhost/test/drop/uploads/banner2.jpg");
});

Now you can use myDropzone.removeAllFiles(); on some event and the files added from server will also get removed from dropzone.

NOTE: Remember when using the above code. if you are firing a server side code to delete the files from server whenever a file is removed from dropzone then all the files that came from the server will get deleted.

like image 195
Vishal Nair Avatar answered Sep 22 '22 12:09

Vishal Nair