简体   繁体   中英

How to disable submit form in Dropzone.js while files are auto uploading

I am using dropzone.js working fine for me. It upload files automatically that's cool..... Now I have a problem, I want to disable submit form button till it upload all the files to the server....

I have called Dropzone with basic code....

jQuery(function () {

                            Dropzone.autoDiscover = false;
                            var myDropzoneOptions = {
                                maxFilesize: 15,
                                addRemoveLinks: true,
                                acceptedFiles: 'image/*, .pdf, .doc, .docx,.xls,.xlsx,.csv,.txt',
        //                                acceptedFiles:'image/* , text/pdf ,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-powerpoint,application/vnd.openxmlformats-officedocument.presentationml.presentation',
                                clickable: true,
                                url: "<?php bloginfo('siteurl') ?>/?my_upload_of_task_files2=1",
                            };

                            var myDropzone = new Dropzone('div#myDropzoneElement2', myDropzoneOptions);

                            myDropzone.on("sending", function (file, xhr, formData) {
                                formData.append("author", "<?php echo $cid; ?>"); // Will send the filesize along with the file as POST data.
                                formData.append("ID", "<?php echo $pid; ?>"); // Will send the filesize along with the file as POST data.
                            });

`

etc that upload files to server....

and sample submit form in HTML

 <form action="..">
  <input type="submit" value="download" />
</form>

You can use the onsending and queuecomplete events to toggle the disabled attribute on the form submit button.

For example:

myDropzone.on("sending", function (file, xhr, formData) {
    // Disable the submit button
    $(":submit").prop("disabled", true);
    formData.append("author", "<?php echo $cid; ?>"); // Will send the filesize along with the file as POST data.
    formData.append("ID", "<?php echo $pid; ?>"); // Will send the filesize along with the file as POST data.
});

myDropzone.on("queuecomplete", function ( ) {
    // Re-enable the submit button
    $(":submit").prop("disabled", false);
});

combine https://www.dropzonejs.com/#dropzone-methods and submit

$(form).submit(function () {
  if (youDropZone.getUploadingFiles().length != 0) {
    return false;
  }
  // if your file field is required additional check if .getAcceptedFiles() == 0
  // if (
  //   youDropZone.getUploadingFiles().length != 0 
  //   || youDropZone.getAcceptedFiles() == 0
  // ) {
  //   return false;
  // }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM