我可以在不上传任何文件的情况下使用 Dropzone 提交表单吗?

Ila*_*Ila 4 php forms jquery dropzone.js

我已经按照将Dropzone 与正常表单结合起来的教程来允许 Dropzone 上传和表单提交。该表格是一个申请表格,它应该在添加和不添加文件的情​​况下都可以使用。目前,它仅在将一个或多个文件添加到 Dropzone 时才起作用。

即使上传队列为空,我是否可以启用允许 Dropzone 处理表单提交的选项?

这是我初始化表单的方式:

                Dropzone.options.general = {
                paramName: 'tx_ddapplicationform_applicationformgeneral[form][files]', // The name that will be used to transfer the file
                autoProcessQueue: false,
                uploadMultiple: true,
                parallelUploads: 100,
                maxFiles: 100,
                addRemoveLinks: true,
                previewsContainer: '.dropzone-previews', // we specify on which div id we must show the files
                clickable: false,


                // The setting up of the dropzone
                init: function() {
                    var myDropzone = this;
                    console.log(myDropzone)
                    console.log("Dropzone init");

                    console.log(this.element.querySelector("input[type=submit]"))

                    // First change the button to actually tell Dropzone to process the queue.
                   this.element.querySelector("input[type=submit]").addEventListener("click", function(e) {
                        // Make sure that the form isn't actually being sent.
                        console.log("the button is clicked")
                        e.preventDefault();
                        e.stopPropagation();
                        myDropzone.processQueue();
                        console.log("after 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() {
                        console.log("sending multiple");
                    });
                    this.on("successmultiple", function(files, response) {
                        console.log("success multiple");
                    });
                    this.on("errormultiple", function(files, response) {
                        console.log("error multiple");
                    });
                }
Run Code Online (Sandbox Code Playgroud)

我浏览了 dropzone.js 表单并添加了 console.logs 以查看发生了什么。成功提交(添加文件)会记录以下内容:

 processQueue dropzone.js:1301
 upload multiple dropzone.js:1314
 sending multiple main.jquery.js:551
 after processQueue main.jquery.js:545
 success multiple main.jquery.js:554
Run Code Online (Sandbox Code Playgroud)

一个不成功的提交,没有附加文件,记录以下内容:

 processQueue dropzone.js:1301
 after processQueue main.jquery.js:545
Run Code Online (Sandbox Code Playgroud)

And*_*rea 5

好吧,可能是一个死灵帖,但由于我在谷歌结果页面的第一个链接中发现了这个问题,并且因为它已经被查看了很多,我认为答案不会伤害任何人。

我有相同的结构并解决我刚刚做的问题:

$('#my-dropzone input[type="submit"]').on('click', function(
    // Make sure that the form isn't actually being sent.
    e.preventDefault();
    e.stopPropagation();
    if (myDropzone.files.length) {
        myDropzone.processQueue(); // upload files and submit the form
    } else {
        $('#my-dropzone').submit(); // just submit the form
    }
});
Run Code Online (Sandbox Code Playgroud)

请注意,如果您使用 dropzone(第一种情况)提交表单,您是通过 ajax 进行的,因此结果应该由返回的响应处理,而如果您只提交表单(第二种情况),您将拥有一个正常的表单提交。

在我的情况下,这需要基于提交类型的不同响应。