dropzone js onclick submit file upload

前端 未结 5 1557
时光说笑
时光说笑 2020-12-24 02:11

upload all files with a single button click.
HTML:

相关标签:
5条回答
  • 2020-12-24 02:24

    I got just finished messing around with this myself- I wanted to add information about the image to a database at the same time as uploading it. Dropping a file opens the input form for the extra info and then the queue needs sending after the form button is pressed.

    I finally achieved this by putting a jquery click event handler inside the init 'on add file' function event:

    this.on("addedfile", function(file){
      var myDropzone = this;
      $('#imageinfoCont').animate({left:'4.5%'});//brings form in
      $('#imgsubbutt').click(function(){
        $('#imageinfoCont').animate({left:'-10000px'}); //hides the form again
        myDropzone.processQueue(); //processes the queue
      });
    });
    

    I am then adding the extra data in a separate 'on sending' function event (could probably do it in the above code but baby steps I think).

    Seems to work like a charm.

    0 讨论(0)
  • 2020-12-24 02:35

    Although this has been answered, I ran into a situation where I only wanted to submit the queue IF it was a certain type of file. The bug I ran into was it was ignoring processQueue.

      this.dropzone = new Dropzone('#my-dropzone', {
        autoProcessQueue: false,
      });
      return this.dropzone.on('addedfile', (function(_this) {
        return function(file) {
    
          var IMAGE_EXTENSIONS, ext;
          IMAGE_EXTENSIONS = 'png jpg jpeg gif'.split(' ');
          ext = (_.last(file.name.split('.'))).toLowerCase();
    
          if (_.include(IMAGE_EXTENSIONS, ext)) {
            return console.log('IMAGE!');
          } else {
    
            return setTimeout(function() { // HERE!!!!!!!!!!!!!!!!!!!!
              return _this.dropzone.processQueue();
            }, 10);
          }
        };
      })(this));
    

    I had to use the setTimeout seen above because processQueue did nothing if I didn't defer it in this manner.

    0 讨论(0)
  • 2020-12-24 02:38

    use simple code

    Dropzone.autoDiscover = false;
    
    var myDropzone = new Dropzone(element, {
      url: "/upload.php",                        
      autoProcessQueue: false,
    });
    
    $('#imgsubbutt').click(function(){           
      myDropzone.processQueue();
    });
    
    0 讨论(0)
  • 2020-12-24 02:39

    Here how i implement delayed uploading (initiated by click on any button, for an example):

    Dropzone implementation

    var count = 0;
    var addedFilesHash = {};
    var myDropzone = new Dropzone("#file_upload-form", {
        paramName: "file", // The name that will be used to transfer the file
        addRemoveLinks: true,
        maxFilesize: 5, // MB
        parallelUploads: 5,
        uploadMultiple: true,
        acceptedFiles: "image/*,.xlsx,.xls,.pdf,.doc,.docx",
        maxFiles: 10,
        init: function() {
            this.on("removedfile", function (file) {
                // delete from our dict removed file
                delete addedFilesHash[file];
            });
        },
        accept: function(file, done) {
            var _id = count++;
            file._id = _id;
            addedFilesHash[_id] = done;
        }
    });
    

    Somewhere else

        // get all uploaded files in array
        var addedFiles = Object.keys(addedFilesHash);
        // iterate them
        for (var i = 0; i< addedFiles.length; i++) {
            // get file obj
            var addedFile = addedFiles[i];
            // get done function
            var doneFile = addedFilesHash[addedFile];
            // call done function to upload file to server
            doneFile();
        }
    

    We override accept and removedFile functions. In accept function we collect file objects and done functions in dict where key is file and value is done function. Later in time, when we are ready to upload added files, we are iterating all done functions for all files in dict addedFilesHash which launches upload progress with progress bar and etc.

    0 讨论(0)
  • 2020-12-24 02:45

    I accomplished this by placing my dropzone in a div instead of a form, thereby removing the ability for dropzone to automatically POST the uploads to a given URL. The URL I passed to the dropzone instance when I created it is literally 'dummy' since it will never be called. For example, HTML

    <button id="submit-all">Submit all files</button>
    <div class="dropzone" id="my-dropzone"></div>
    

    JavaScript

    $('#submit-all').on('click', function() {
        var files = $('#my-dropzone').get(0).dropzone.getAcceptedFiles();
        // Do something with the files.
    });
    
    0 讨论(0)
提交回复
热议问题