After normal form with dropzone

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> <!-- this is were the previews should be shown. -->

  <!-- Now setup your input fields -->
  <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 = { // The camelized version of the ID of the form element

  // The configuration we've talked about above
  autoProcessQueue: false,
  uploadMultiple: true,
  parallelUploads: 100,
  maxFiles: 100,

  // The setting up of the dropzone
  init: function() {
    var myDropzone = this;

    // First change the button to actually tell Dropzone to process the queue.
    this.element.querySelector("button[type=submit]").addEventListener("click", function(e) {
      // Make sure that the form isn't actually being sent.
      e.preventDefault();
      e.stopPropagation();
      myDropzone.processQueue();
    });

    // Listen to the sendingmultiple event. In this case, it the sendingmultiple event instead
    // of the sending event because uploadMultiple is set to true.
    this.on("sendingmultiple", function() {
      // Gets triggered when the form is actually being sent.
      // Hide the success button or the complete form.
    });
    this.on("successmultiple", function(files, response) {
      // Gets triggered when the files have successfully been sent.
      // Redirect user or notify of success.
    });
    this.on("errormultiple", function(files, response) {
      // Gets triggered when there was an error sending the files.
      // Maybe show form again, and notify user of error
    });
  }

}

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([]); //send empty 
                        }                                    
                    }        

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)
        {
            // Here I get Request.IsAjaxRequest() = true when form is submitted
            if (ModelState.IsValid)
            {
                var container = DoSomething();
                if (container.HasErrors)
                {
                    SetError(container.ErrorMessage);
                    return RedirectToAction("Index");
                }
            }
            else
            {
                SetAlert("ErrorMessage");
            }
            return RedirectToAction("Index");
        }

? .

+4
2

. ...

2 ;

- ;

return Json(new { ErrorMessage = "", RedirectURL = Url.Action("Get", null, new { id = result.Value.id }, Request.Url.Scheme ) });

JS, :

this.on("successmultiple", function (files, response) {
                // Gets triggered when the files have successfully been sent.
                // Redirect the user or notify of success.
                var errorMessage = response.ErrorMessage;
                if (errorMessage.length > 0) {
                    alert(errorMessage);
                }
                else {
                    window.location.replace(response.RedirectURL);
                }
};
+2

Dropzone AJAX, , , . :

  • JSON URL- .

  1. AJAX. Dropzone.
0

Source: https://habr.com/ru/post/1613865/


All Articles