Dropzone检查图像尺寸和文件大小 [英] Dropzone check image dimension and file size

查看:363
本文介绍了Dropzone检查图像尺寸和文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using the Dropzonejs plugin. I want to check the image dimension (width and height) and also the file size when a file is uploaded. I managed to check the dimension and file size but when I combined both of them, it didn't work well.

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;
    }
}

如果:


  • 上传大尺寸图片:它接受功能

  • upload big dimension image: it accepts the function

上传小尺寸图片,但2MB:它将返回 file.acceptDimensions不是函数或不会成功

upload small dimension image but more than 2MB: it will return file.acceptDimensions is not a function or will not go to success

推荐答案

如果有人仍然需要答案。

In case someone still need the answer.

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();
            }
        }
    })
},

这篇关于Dropzone检查图像尺寸和文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆