首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在dropzone.js中限制最大“总”文件大小?

问如何在dropzone.js中限制最大“总”文件大小?
EN

Stack Overflow用户
提问于 2015-06-30 02:18:39
回答 4查看 15.7K关注 0票数 4

目前,我使用下拉区域来处理Jquery中的文件上传。到目前为止还不错。

唯一的问题是,在配置中有一个maxFileSize选项,它限制了“单一”文件的大小。

由于服务器(php.ini)也有“总”文件大小的限制,我想知道如何在dropzone.js中限制它?

非常感谢。

http://www.dropzonejs.com/#configuration-options

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2015-06-30 02:59:14

我只看到maxfilesize,paralleluploads和maxfiles

我认为您可能需要在添加文件时跟踪它们的大小,可能需要使用

代码语言:javascript
复制
this.on("addedfile", function(file) { // perform task // });

..。记录文件大小,如果超过总文件大小,则禁用“提交”按钮。

代码语言:javascript
复制
document.getElementById('dropsubmit').disabled = false;

在添加和删除文件时,可以使用"addedFile“和"removedFile”事件来更改跟踪文件大小的变量。根据累积大小,将submit按钮设置为true或false。

票数 4
EN

Stack Overflow用户

发布于 2019-04-02 09:26:24

我找到了定义accept函数的解决办法:

代码语言:javascript
复制
var totalsize = 0.0;
$("div#dropzone").dropzone({ 
    accept: function(file, done) {
        if (totalsize >= MAX_TOTAL_SIZE) {
            file.status = Dropzone.CANCELED;
            this._errorProcessing([file],  "Max limit reached", null);
        }else { 
            done();
        }
    }
    init: function() {
        this.on("addedfile", function(file) { 
            totalsize += parseFloat((file.size / (1024*1024)).toFixed(2));
        });
        this.on("removedfile", function(file) {
            if(file.upload.progress != 0){
                totalsize -= parseFloat((file.size / (1024*1024)).toFixed(2));
            }
        });
        this.on("error", function(file) {
            totalsize -= parseFloat((file.size / (1024*1024)).toFixed(2));
        });
    }
}); 

如果MAX_TOTAL_SIZE是该下拉区域的最大值,则该区域不应超过。

票数 3
EN

Stack Overflow用户

发布于 2017-09-15 19:02:22

现在有点晚了,但也许其他人需要这个。您需要在init函数中创建一个新变量"totalSize“。将事件侦听器添加到fileAdd以增加大小,并添加另一个事件侦听器以进行子结构,然后在发送显示错误的请求之前进行一点控制,我对英语很差,下面是一个示例:

代码语言:javascript
复制
...
init: function() {
   var totalsize = 0.0;
 ...
    this.on("addedfile", function(file) {
 ... 
// increment total size when we add a file (in Mb)
    totalsize += parseFloat((file.size / (1024*1024)).toFixed(2));

//substruct the size of the removed file
      removeButton.addEventListener("click", function(e) {
      ...
         _this.removeFile(file);
      totalsize -= parseFloat((file.size / (1024*1024)).toFixed(2));
     ...                      
      });
  ...             
  });
//and an event for the submission to controle before submit don't forget to prevent default
this.on("sendingmultiple", function(data, xhr, formData) {

if (totalsize <= 20) {
      //do the request
       }else { // else show the error
        $('#error').show();
       $('#error').text('Oooops ! total files size must be less then 20Mb');

                        }
                  });
}

也许没有那么清楚,所以有一个完整的代码示例,在我的代码中我添加了一个样式化的remove按钮,所以不要忘记删除它:

代码语言:javascript
复制
init: function() {
                    var totalsize = 0.0;
                    dzClosure = this; // Makes sure that 'this' is understood inside the functions below.

                      // for Dropzone to process the queue (instead of default form behavior):
                      document.getElementById("submit-all").addEventListener("click", function(e) {
                          // Make sure that the form isn't actually being sent.
                          e.preventDefault();
                          e.stopPropagation();
                          dzClosure.processQueue();
                      });
                    this.on("addedfile", function(file) {
                        // Create the remove button
                        var removeButton = Dropzone.createElement("<a href='javascript:;'' class='btn red btn-sm btn-block'>Remove</a>");

                        // Capture the Dropzone instance as closure.
                        var _this = this;

                        // Listen to the click event
                        removeButton.addEventListener("click", function(e) {
                          // Make sure the button click doesn't submit the form:
                          e.preventDefault();
                          e.stopPropagation();

                          // Remove the file preview.
                          _this.removeFile(file);
                          totalsize -= parseFloat((file.size / (1024*1024)).toFixed(2));
                          // If you want to the delete the file on the server as well,
                          // you can do the AJAX request here.
                        });

                        // Add the button to the file preview element.
                        file.previewElement.appendChild(removeButton);
                        //increment
                        totalsize += parseFloat((file.size / (1024*1024)).toFixed(2));


                    });

                                    //send all the form data along with the files:
                      this.on("sendingmultiple", function(data, xhr, formData) {

                          if (totalsize <= 20) {
                            console.log(totalsize);
        //u can append formData here too
                            formData.append("something", jQuery("#something").val());
                          }else {
                              $('#error').show();
                              $('#error').text('Oooops ! total files size must be less then 20Mb');

                            }
                      });
                } 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31128505

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档