【问题标题】:Dropzone check image dimension and file sizeDropzone 检查图像尺寸和文件大小
【发布时间】:2016-08-03 08:04:30
【问题描述】:

我正在使用 Dropzonejs 插件。我想检查图像尺寸(宽度和高度)以及上传文件时的文件大小。我设法检查了尺寸和文件大小,但是当我将它们结合起来时,效果不佳。

var maxImageWidth = 2500, 
    maxImageHeight = 2500;

Dropzone.options.formUserEdit = {
    maxFilesize: 2,
    acceptedFiles: 'image/*',
    success : function(file, Response){
      var obj = JSON.parse(Response);
      $('#form-user-edit').append('<input type="hidden" name="userprofile" data-ori="'+file.name+'" value="'+obj.filename+'" />');

      $('.error-msg').remove();
    },
    init: function() {
        this.on("thumbnail", function(file) {
            if (file.width > maxImageWidth || file.height > maxImageHeight) {
                file.rejectDimensions();
            else {
                file.acceptDimensions();
            }
        })
    },
    accept: function(file, done) {
        file.rejectDimensions = function() { 
            done("Please make sure the image width and height are not larger than 2500px."); 
        };
        file.acceptDimensions = done;
    }
}

如果:

  • 上传大尺寸图片:接受函数

  • 上传小尺寸图片但超过2MB:会返回file.acceptDimensions is not a function或者不会成功

【问题讨论】:

    标签: jquery dropzone.js


    【解决方案1】:

    如果有人仍然需要答案。

    init: function() {
        this.on("thumbnail", function(file) {
            if (file.width > maxImageWidth || file.height > maxImageHeight) {
                file.rejectDimensions();
            else {
                if(file.size < 1024*1024*2/*2MB*/)
                {
                    file.acceptDimensions();
                }
            }
        })
    },
    

    【讨论】:

    • 有趣,所以当我尝试在添加文件时获取宽度时,它似乎失败了,它仅在拇指生成事件之后才有效。很高兴知道。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-07
    • 1970-01-01
    • 2013-07-20
    • 1970-01-01
    • 2010-11-21
    • 1970-01-01
    相关资源
    最近更新 更多