dropzone.js - DropZonejs : Submit form without files

标签 dropzone.js

我已经成功地将 dropzone.js 集成到现有表单中。此表单发布附件和其他输入,如复选框等。

当我提交带有附件的表单时,所有输入都正确发布。但是,我想让用户可以在没有任何附件的情况下提交表单。除非有附件,否则 Dropzone 不允许提交表单。

有人知道我如何覆盖这个默认行为并提交不带任何附件的 dropzone.js 表单吗?谢谢!

   $( document ).ready(function () {
    Dropzone.options.fileUpload = { // The camelized version of the ID of the form element

      // The configuration we've talked about above
      autoProcessQueue: false,
      uploadMultiple: true,
      parallelUploads: 50,
      maxFiles: 50,
      addRemoveLinks: true,
      clickable: "#clickable",
      previewsContainer: ".dropzone-previews",
      acceptedFiles: "image/*,application/pdf, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.openxmlformats-officedocument.spreadsheetml.template, application/vnd.openxmlformats-officedocument.presentationml.template, application/vnd.openxmlformats-officedocument.presentationml.slideshow, application/vnd.openxmlformats-officedocument.presentationml.presentation, application/vnd.openxmlformats-officedocument.presentationml.slide, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/vnd.openxmlformats-officedocument.wordprocessingml.template, application/vnd.ms-excel.addin.macroEnabled.12, application/vnd.ms-excel.sheet.binary.macroEnabled.12,text/rtf,text/plain,audio/*,video/*,.csv,.doc,.xls,.ppt,application/vnd.ms-powerpoint,.pptx",



        // 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's 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) {
            window.location.replace(response.redirect);
            exit();
        });
        this.on("errormultiple", function(files, response) {
          $("#notifications").before('<div class="alert alert-error" id="alert-error"><button type="button" class="close" data-dismiss="alert">×</button><i class="icon-exclamation-sign"></i> There is a problem with the files being uploaded. Please check the form below.</div>');
          exit();
        });

      }

    }
  });

最佳答案

使用以下内容:

$('input[type="submit"]').on("click", function (e) {

                    e.preventDefault();
                    e.stopPropagation();

                    var form = $(this).closest('#dropzone-form');
                    if (form.valid() == true) { 
                        if (myDropzone.getQueuedFiles().length > 0) {                        
                            myDropzone.processQueue();  
                        } else {                       
                            myDropzone.uploadFiles([]); //send empty 
                        }                                    
                    }               
                });

引用:https://github.com/enyo/dropzone/issues/418

关于dropzone.js - DropZonejs : Submit form without files,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20910571/

相关文章:

javascript - 使用 dropzone 上传文件后显示成功消息

laravel - DropZone.js 上传无法以编程方式进行

javascript - 在 Dropzone.js 中将 ID 添加到预览 div

javascript - 使用 dropzonejs-rails 和选项

javascript - addRemoveLinks 在我的 Dropzone 上不起作用

ios - Dropzone 在 iOS 11 上失败

javascript - Dropzone.js - 上传后的成功消息

javascript - Dropzone图片上传错误显示如何去除错误

dropzone.js - 成功后从dropzone删除预览

dropzone.js - Dropzone 接受文件错误