DropZonejs: Submit form without files

后端 未结 7 1700
一生所求
一生所求 2020-12-05 14:27

I\'ve successfully integrated dropzone.js inside an existing form. This form posts the attachments and other inputs like checkboxes, etc.

When I submit the form wit

相关标签:
7条回答
  • 2020-12-05 14:42

    I tried Matija Grcic's answer and I got the following error:

    Uncaught TypeError: Cannot read property 'name' of undefined
    

    And I didn't want to modify the dropzone source code, so I did the following:

            if (myDropzone.getQueuedFiles().length > 0) {                        
                myDropzone.processQueue();  
            } else {                       
                myDropzone.uploadFiles([{name:'nofiles'}]); //send empty 
            }                                    
    

    Note: I'm passing an object inside the array to the uploadFiles function.

    Then I check server-side, if name != 'nofiles' do upload stuff.

    0 讨论(0)
  • 2020-12-05 14:46

    The first approach is kind of too expensive for me, I would not like to dive into the source code and modify it,

    If you happen to be like me , use this.

    function submitMyFormWithData(url)
        {
            formData = new FormData();
            //formData.append('nameOfInputField', $('input[name="nameOfInputField"]').val() );
    
            $.ajax({
                    url: url,
                    data: formData,
                    processData: false,
                    contentType: false,
                    type: 'POST',
    
                    success: function(data){
                    alert(data);
                    }
            });
        }
    

    And in your dropzone script

    $("#submit").on("click", function(e) {
                          // Make sure that the form isn't actually being sent.
                          e.preventDefault();
                          e.stopPropagation();
    
                            if (myDropzone.getQueuedFiles().length > 0)
                            {                        
                                    myDropzone.processQueue();  
                            } else {                 
                                    submitMyFormWithData(ajaxURL);
                            }     
    
                        });
    
    0 讨论(0)
  • 2020-12-05 14:47

    Depending on your situation you could simply submit the form:

    if (myDropzone.getQueuedFiles().length > 0) {                        
       myDropzone.processQueue();  
    } else {                       
       $("#my_form").submit();
    }
    
    0 讨论(0)
  • 2020-12-05 14:59

    Pretty simple, you stop the propagation ONLY if you have files to be submitted via Dropzone:

    // First change the button to actually tell Dropzone to process the queue.
    this.element.querySelector("button[type=submit]").addEventListener("click", function(e) {
      // Stop the propagation ONLY if you have files to be submitted via Dropzone
      if (myDropzone.getQueuedFiles().length > 0) {
            e.preventDefault();
            e.stopPropagation();
            myDropzone.processQueue();
      }
    });
    
    0 讨论(0)
  • 2020-12-05 15:04

    Use the following:

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

    Reference: https://github.com/enyo/dropzone/issues/418

    0 讨论(0)
  • 2020-12-05 15:08

    You should check if there are files in the queue. If the queue is empty call directly dropzone.uploadFile(). This method requires you to pass in a file. As stated on [caniuse][1], the File constructor isn't supported on IE/Edge, so just use Blob API, as File API is based on that.

    The formData.append() method used in dropzone.uploadFile() requires you to pass an object which implements the Blob interface. That's the reason why you cannot pass in a normal object.

    dropzone version 5.2.0 requires the upload.chunked option

    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();
    }
    
    0 讨论(0)
提交回复
热议问题