简体   繁体   English

我可以使用Dropzone提交表格而无需上传任何文件吗?

[英]Can I submit a form with Dropzone without uploading any files?

I've followed the Combine Dropzone With Normal Form tutorial to allow Dropzone uploads & form submit. 我遵循了Combine Dropzone和Normal Form教程,允许Dropzone上传和表单提交。 The form is an application form, which should work both with & without files added. 表单是一个申请表,无论是否添加文件都可以使用。 Currently it works only when one or more files are added to the Dropzone. 目前,仅当将一个或多个文件添加到Dropzone时,它才有效。

Is there an option I can enable to allow Dropzone to process the form submission even if the upload queue is empty? 是否有一个选项我可以允许Dropzone处理表单提交,即使上传队列为空?

Here's how I initialise the form: 以下是我初始化表单的方法:

                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");
                    });
                }

I went through the dropzone.js form and added console.logs to see what was going on. 我浏览了dropzone.js表单并添加了console.logs来查看发生了什么。 A successful submit (with a file added) logs this: 成功提交(添加了文件)记录下:

 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

An unsuccessful submit, without an attached file, logs this: 没有附加文件的不成功提交会记录下:

 processQueue dropzone.js:1301
 after processQueue main.jquery.js:545

Ok, probably a necro post but since I'found this question in the first links of Google result page and since it has been viewed quite a lot I think that an answer will harm no one. 好吧,可能是一个死灵帖子,但是因为我在谷歌结果页面的第一个链接中发现了这个问题,并且因为它被观看了很多,我认为答案不会伤害任何人。

I have tha same structure and to solve the problem I just do: 我有相同的结构,并解决我刚才做的问题:

$('#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
    }
});

Mind that if you submit the form with dropzone (1st case) you are doing it through ajax so the result should be handled by the returned response, while, if you submit just the form (2nd case) you are going to have a normal form submit. 请注意,如果您使用dropzone(第一种情况)提交表单,则通过ajax执行此操作,因此结果应由返回的响应处理,而如果您只提交表单(第二种情况),则您将获得正常的表单提交。

In my case this required a different response based on the type of submission. 在我的情况下,这需要基于提交类型的不同响应。

You should check if there are files in the queue. 您应该检查队列中是否有文件。 If the queue is empty call directly dropzone.uploadFile(). 如果队列为空,则直接调用dropzone.uploadFile()。 This method requires you to pass in a file. 此方法要求您传入文件。 As stated on caniuse , the File constructor isn't supported on IE/Edge, so just use Blob API, as File API is based on that. 正如caniuse所述,IE / Edge上不支持File构造函数,因此只需使用Blob API,因为File API就是基于此。

The formData.append() method used in dropzone.uploadFile() requires you to pass an object which implements the Blob interface. dropzone.uploadFile()中使用的formData.append()方法要求您传递实现Blob接口的对象。 That's the reason why you cannot pass in a normal object. 这就是为什么你不能传入普通对象的原因。

dropzone version 5.2.0 requires the upload.chunked option dropzone版本5.2.0需要upload.chunked选项

if (this.dropzone.getQueuedFiles().length === 0) {
    var blob = new Blob();
    blob.upload = { 'chunked': this.dropzone.defaultOptions.chunking };
    this.dropzone.uploadFile(blob);
} else {
    this.dropzone.processQueue();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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