Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery : a drag-and-drop upload with multi dropzone

I want to develop a tree of folders and files with a drag-and-drop upload on folders.

Example:

enter image description here

For the drag-and-drop upload, I fund jQuery File Upload.

The basic code is:

$('#fileupload').fileupload({
    dataType: 'json',
    url: 'php/index.php',
    dropZone: $(document),
    done: function (e, data) {
        $.each(data.result, function (index, file) {
            $('<p/>').text(file.name).appendTo(document.body);
        });
    }
});

In my project, I've multi dropzone (my folders) like for example :

<ul>
    <li>Bookmarks</li>
    <li>Search</li>
    <li>Web dev</li>
    ...
</ul>

How to change the code above to handle the multi dropzone with id, class and jQuery selectors?


I tried something like this but obviously it doesn't work:

<ul>
    <li id="folder1" class="folder">Bookmarks</li>
    <li id="folder2" class="folder">Search</li>
    <li id="folder3" class="folder">Web dev</li>
    ...
</ul>

.

$('.folder').fileupload({
    dataType: 'json',
    url: 'php/index.php',
    dropZone: $('.folder'),
    done: function (e, data) {
        $.each(data.result, function (index, file) {
            $('<p/>').text($(this).attr(id)).appendTo(document.body);
        });
    }
});

Issues:

  • the upload is done 3 times.
  • $(this) doesn't exist.
like image 525
GG. Avatar asked Dec 23 '11 13:12

GG.


1 Answers

I found a solution with a colleague.

Actually it wasn't complicated:

    $('.folder').each(function(){
        var $this = $(this);

        $this.fileupload({
            dataType: 'json',
            url: 'php/index.php',
            dropZone: $this,
            done: function (e, data) {
                $.each(data.result, function (index, file) {
                    $('<p/>').text($this.attr('id')).appendTo(document.body);
                });
            }
        });
    });

Edit: My bad, this answer was in the documentation.

like image 182
GG. Avatar answered Nov 09 '22 07:11

GG.