4

I want to have multiple dropzones in a form. So I created a form

<form method="post">
    <div class="upload-files" data-name="mainImages[]" />
    <div class="upload-files" data-name="secImages[]" />
    <!-- could also be more -->

    <input type="text" name="test" />

    <input type="submit" />
</form>

The dropzones are initialized with its own paramName.

var dropzones = [];
$('.upload-files').each(function() {
    dropzones.push(new Dropzone('#' + $dropzone.attr('id'), {
       paramName: $(this).data('name'),
       // ...
    }
);

this.dropzones = dropzones;

How to submit multiple dropzones with the form data in one request? Currently it look like this on submit.

// submit
if (this.dropzones.length) {
    return true; // normal form submit without dropzone
}

// dropzone submit form
for (var i = 0, length = this.dropzones.length; i < length; i++) {
    // TODO combine files with correct paramNames
}

I know this https://github.com/enyo/dropzone/wiki/Combine-normal-form-with-Dropzone but this is only for one dropzone in one form.

What I think I need todo is to add the files from the second, third, ... dropzone to the first when submitting but I dont know how to handle that.

1 Answer 1

6

I found a solution to work with multiple dropzones, on the form submit you need to do the following.

if (!this.dropzones.length) {
    // default form submit
    return true;
}

// submit over dropzone
event.preventDefault();
this.dropzones[0].processQueue();

return false;

This will process the first dropzone. Now we need to add the other dropzones files to the submit in the sendingmultiple event

init: function() {
    this.on('sendingmultiple', function(data, xhr, formData) {
        // add other form fields
        $.each($form.serializeArray(), function(index, item) {
            formData.append(item.name, item.value);
        });

        // add other dropzone files
        for (var i = 1, length = this.dropzones.length; i < length; i++) {
            var files = this.dropzones[i].getQueuedFiles();

            for (var x = 0, fileLength = files.length; x < fileLength; x++) {
                formData.append(
                    this.dropzones[i]._getParamName(x),
                    files[x],
                    files[x].name
                );
            }
        }
    });
}
2
  • How about custom options per upload input such as one accepts only images, the other zip file and one has max files 1 the other max files 5 etc. Commented Mar 15, 2019 at 13:24
  • Hi there! you wiill need to find a way to specify the type for each input, for example: <div class="upload-files" data-name="mainImages[]" data-type="image/jpg" />. Then set acceptedFiles: of the dropzone accordingly
    – Patrick
    Commented Aug 18, 2021 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.