【问题标题】:Dropzone not validating maximum number of filesDropzone 未验证最大文件数
【发布时间】:2016-08-25 11:51:06
【问题描述】:

我正在使用 dropzone 上传文件,我已将文件限制设置为最多六个文件,如果我一次上传一张图片,此代码有效,但如果我在开始时按控制按钮选择超过六个图片上传文件然后它不会验证文件并上传所有文件。我使用 laravel 作为后端,我的代码是:-

Dropzone.options.myAwesomeDropzone = {
    paramName: "file", // The name that will be used to transfer the file
    maxFilesize: 1, // MB
    maxFiles: 6,
    acceptedFiles: ".jpeg,.jpg,.png,.gif",
    clickable: true,
    init: function () {
        this.on("success", function(file, responseText) {
            file.previewTemplate.setAttribute('id',responseText[0].id);
        });
        this.on("thumbnail", function(file) {
            if (file.width < 350 || file.height < 200) {
                file.rejectDimensions()
            }
            else {
                file.acceptDimensions();
            }
        });
    },
    accept: function(file, done) {
        file.acceptDimensions = done;
        file.rejectDimensions = function() { done("Image width or height should be greater than 350*200"); };
    },
    removedfile: function(file){
        var name = file.name; 
        $.ajax({
                type: 'POST',
                url: ajax_url+'listing/deleteListingImage/'+name,
                dataType: 'html'
            });
            var _ref;
            return (_ref = file.previewElement) != null ? _ref.parentNode.removeChild(file.previewElement) : void 0;
            },   
            dictDefaultMessage: "Drop your files to upload"
    }

谢谢

【问题讨论】:

  • A. console 中的任何错误? B. 你读过docs about it 吗?不是验证,只是调用事件maxfilesexceeded
  • 它的验证很好,唯一的问题是如果我一次上传超过六张图片
  • 在控制台也没有给出任何错误

标签: javascript jquery html dropzone.js


【解决方案1】:

您可以在调用maxfilesexceeded 时使用方法removeFile(file) 删除多余的文件。

像这样:

Dropzone.options.myAwesomeDropzone = {
  maxFiles: 2,
  autoProcessQueue: false,
  init: function(){
    var myDropZone = this;
    myDropZone.on('maxfilesexceeded', function(file) {
      myDropZone.removeFile(file);
    });
  }
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/dropzone/4.3.0/dropzone.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/4.3.0/min/dropzone.min.js"></script>
<form action="/file-upload"
      class="dropzone"
      id="my-awesome-dropzone">

</form>

http://output.jsbin.com/dodajij

【讨论】:

  • 为什么要设置autoProcessQueue : false,是不是为了不自动上传文件?因为当我们添加此代码时,没有任何反应,如果我们删除 autoprocessQueue,它仍在上传所有文件,而不是删除额外文件。
  • 当然:autoProcessQueue: false 这只是为了演示,因为我没有服务器来处理请求表单。你应该删除它。
  • 我知道错误在哪里,我们在代码中使用了接受函数来验证文件的尺寸,正如我在代码中提到的那样,如果我删除了该接受函数,那么您的代码可以正常工作我。是否有任何替代接受功能?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-23
  • 2016-09-22
相关资源
最近更新 更多