Dropzone
Search…
Combine form data with files
Create a form with various other input fields that also acts as a Dropzone.
You want to have a normal form, that you can drop files in, and send the whole form, including the files, with the click of a button? Well, you're in luck. It requires a bit of configuration, but it's not difficult to do.
First off, you need to make sure that Dropzone won't auto process your queue and start uploading each file individually. This is done by setting these properties:
  • autoProcessQueue: false Dropzone should wait for the user to click a button to upload
  • uploadMultiple: true Dropzone should upload all files at once (including the form data) not all files individually
  • parallelUploads: 100 that means that they shouldn't be split up in chunks as well
  • maxFiles: 100 and this is to make sure that the user doesn't drop more files than allowed in one request.
This is basically everything you need to do. Dropzone automatically submits all input fields inside a form that behaves as a Dropzone.

Show me the code

This is what a very simple HTML looks like:
1
<form id="upload-form" class="dropzone">
2
<!-- this is were the previews should be shown. -->
3
<div class="previews"></div>
4
5
<!-- Now setup your input fields -->
6
<input type="email" name="username" />
7
<input type="password" name="password" />
8
​
9
<button type="submit">Submit data and files!</button>
10
</form>
Copied!
And now for the Dropzone configuration:
1
Dropzone.options.uploadForm = { // The camelized version of the ID of the form element
2
​
3
// The configuration we've talked about above
4
autoProcessQueue: false,
5
uploadMultiple: true,
6
parallelUploads: 100,
7
maxFiles: 100,
8
​
9
// The setting up of the dropzone
10
init: function() {
11
var myDropzone = this;
12
​
13
// First change the button to actually tell Dropzone to process the queue.
14
this.element.querySelector("button[type=submit]").addEventListener("click", function(e) {
15
// Make sure that the form isn't actually being sent.
16
e.preventDefault();
17
e.stopPropagation();
18
myDropzone.processQueue();
19
});
20
​
21
// Listen to the sendingmultiple event. In this case, it's the sendingmultiple event instead
22
// of the sending event because uploadMultiple is set to true.
23
this.on("sendingmultiple", function() {
24
// Gets triggered when the form is actually being sent.
25
// Hide the success button or the complete form.
26
});
27
this.on("successmultiple", function(files, response) {
28
// Gets triggered when the files have successfully been sent.
29
// Redirect user or notify of success.
30
});
31
this.on("errormultiple", function(files, response) {
32
// Gets triggered when there was an error sending the files.
33
// Maybe show form again, and notify user of error
34
});
35
}
36
37
}
Copied!
That's it.
Last modified 2mo ago
Copy link