2014-04-29 50 views
2

我已經按照Combine Dropzone With Normal Form教程允許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"); 
        }); 
       } 

我通過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 

不成功的提交,無需附加的文件,記錄此:

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

可能重複:[DropZonejs:提交表單沒有文件(http://stackoverflow.com/questions/20910571/dropzonejs-submit-form -without-files?rq = 1) –

+0

你如何在服務器端接收圖像?我使用PHP和我打印$ _FILES,但它是空的。我不知道該去哪找... – Limon

回答

2

好吧,可能是死靈職位,但因爲這I'found在谷歌結果頁面的第一個鏈接中提出問題,並且由於它已被觀看了很多,我認爲答案不會傷害任何人。

我有塔相同的結構,解決問題我只是做:

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

記住,如果你提交與懸浮窗(第一種情況),您是通過AJAX做的表單,這樣的結果應該如何處理通過返回的回覆,而如果您僅提交表單(第二種情況),您將要提交正常表單。

在我的情況下,這需要基於提交類型的不同響應。

1

您應該檢查隊列中是否有文件。如果隊列爲空,則直接調用dropzone.uploadFile()。這種方法要求你傳入一個文件。如caniuse所述,文件構造函數在IE/Edge上不受支持,因此只需使用Blob API,因爲File API基於此。

dropzone.uploadFile()中使用的formData.append()方法要求您傳遞實現Blob接口的對象。這就是爲什麼你不能傳遞正常對象的原因。

懸浮窗版本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(); 
} 
相關問題