How to calculate all files size uploaded in dropzo

2020-04-21 08:02发布

I wanted to calculate all selected files total size uploaded in dropzone.

Ex : if I selected 5 files each of size 2mb then it should return 10mb total size

I wanted to restrict if total size of all files is greater than limit.

Can anyone please help me in this I am really stucked.

3条回答
小情绪 Triste *
2楼-- · 2020-04-21 08:19

you may need a full version of this function.

var myDropzone = new Dropzone("#my-dropzone");
var totalSizeLimit = 300*1024*1024; //300MB
myDropzone.on("uploadprogress", function(file, progress, bytesSent) {
    var alreadyUploadedTotalSize = getTotalPreviousUploadedFilesSize();
    if((alreadyUploadedTotalSize + bytesSent) > totalSizeLimit){
      this.disable();
    }
});
function getTotalPreviousUploadedFilesSize(){
   var totalSize = 0;
   myDropzone.getFilesWithStatus(Dropzone.SUCCESS).forEach(function(file){
      totalSize = totalSize + file.size;
   });
   return totalSize;
}
查看更多
Deceive 欺骗
3楼-- · 2020-04-21 08:20

If you set uploadMultiple: true, below method runs just once when files dropped, no matter how many files. Using sendingmultiple is a little optimization since it runs just one time per queue, not for every single file.

init: function() {

  this.on("sendingmultiple", function(file) {

    var uploaded_size = 0;

    $.each(file, function(k, v) {
      uploaded_size += v.size;
    });

    if (uploaded_size > 10240000) {

      alert('Total size of all files: ' + uploaded_size);

      this.disable(); // disable current upload
      this.enable(); // enable drop zone again
    }

  });

}
查看更多
叼着烟拽天下
4楼-- · 2020-04-21 08:32

First get files uploaded successfully, then iterate through them and get size. You can then call disable() on the dropzone if the total exceeds a certain limit.

var files = dropzoneElement.getFilesWithStatus(Dropzone.SUCCESS), total=0;
$.each(files, function(file){
   total+=file.size;
});
alert(total);
查看更多
登录 后发表回答