Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropzone.js and ajax

Im using dropzone.js and loading it using ajax.

My menu ID = "#menu" The upload file should appear in "#div1"

The callback function is not working. I replaced Dropzone.discover by alert("test");

(document).ready(function() {

    $("#menu").click(function(){

    $("#div1").load("upload.php",null, function(){
        Dropzone.discover();
    });

Note: I tried the code below, but it didnt work.

$("#div1").load("upload.php", function(){
    Dropzone.discover();
});
like image 452
Randy Geily Avatar asked Feb 25 '14 14:02

Randy Geily


People also ask

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.

How do I retrieve files from dropzone?

To access all files in the dropzone, use myDropzone. files . If you do not need a dropzone anymore, just call . disable() on the object.


2 Answers

I had a problem getting a Dropzone that was loaded via Ajax to work and discovered that adding the Dropzone.discover(); call fixed it for me.

like image 73
Dan Herman Avatar answered Sep 19 '22 17:09

Dan Herman


You should define dropzone on #dive and add your events in init function of dropzone to change it's options related to each #menu. it's best way.

fore example:

var myDropzone = new Dropzone("#div1",{
    url: '/test.php',
    acceptedFiles: "image/*",
    addRemoveLinks: true,
    removedfile: function(file) {
        $.get('remove.php',function(data){
            var _ref;
            return (_ref = file.previewElement) != null ? _ref.parentNode.removeChild(file.previewElement) : void 0;
        });
    },
    init: function() {
        var thisDropzone = this;
        $('body').on('click','a.menu',function(event){
            href = $(this).attr('href');

            thisDropzone.options.url = href;
        });

        $("body").on('click','#btnRemoveAll',function () {
                thisDropzone.removeAllFiles();
            }
        );

    }
});
like image 30
Mehrdad Dadkhah Avatar answered Sep 21 '22 17:09

Mehrdad Dadkhah