I followed this tutorial to enable DropZone with elements of a traditional form:
HTML
<form id="my-awesome-dropzone" class="dropzone">
<div class="dropzone-previews"></div>
<input type="email" name="username" />
<input type="password" name="password" />
<button type="submit">Submit data and files!</button>
</form>
And js here
Dropzone.options.myAwesomeDropzone = {
autoProcessQueue: false,
uploadMultiple: true,
parallelUploads: 100,
maxFiles: 100,
init: function() {
var myDropzone = this;
this.element.querySelector("button[type=submit]").addEventListener("click", function(e) {
e.preventDefault();
e.stopPropagation();
myDropzone.processQueue();
});
this.on("sendingmultiple", function() {
});
this.on("successmultiple", function(files, response) {
});
this.on("errormultiple", function(files, response) {
});
}
}
Works well unless the user has uploaded files. According to this post , I have to make some changes:
Replace simple
myDropzone.processQueue();
by
var form = $(this).closest('#dropzone-form');
if (form.valid() == true) {
if (myDropzone.getQueuedFiles().length > 0) {
myDropzone.processQueue();
} else {
myDropzone.uploadFiles([]);
}
}
Now, as written in the stackoverflow message, the message "DropZonejs: Submit form without files", I get
Uncaught TypeError: Cannot read property 'status' of undefined
So, I checked dropzone issue 687 which fix this by replacing some of the contents of dropzone.js. This line
ata.append(this._getParamName(i), files[i], files[i].name);
to these lines
if ( typeof files[i] != "undefined" ) {
formData.append(this._getParamName(i), files[i], files[i].name);
} else {
formData.append(this._getParamName(i), "");
}
( ). , , - AJAX, , . Json URL , .
:
[HttpPost]
public ActionResult Create(CustomViewModel model)
{
if (ModelState.IsValid)
{
var container = DoSomething();
if (container.HasErrors)
{
SetError(container.ErrorMessage);
return RedirectToAction("Index");
}
}
else
{
SetAlert("ErrorMessage");
}
return RedirectToAction("Index");
}
?
.