JQuery正在验证大小为<;的图像;2mb

JQuery正在验证大小为<;的图像;2mb,jquery,Jquery,我想用jQuery验证我的输入上传图像,只允许大小小于2mb的图像。我到处都能找到,但大部分指南都是关于验证尺寸(图像的宽度和高度)的。以下是我的代码: html: 试试这个 if(Math.round(file.size/(1024*1024)) > 2){ // make it in MB so divide by 1024*1024 alert('Please select image size less than 2 MB'); return false; }

我想用jQuery验证我的输入上传图像,只允许大小小于2mb的图像。我到处都能找到,但大部分指南都是关于验证尺寸(图像的宽度和高度)的。以下是我的代码:

html:

试试这个

 if(Math.round(file.size/(1024*1024)) > 2){ // make it in MB so divide by 1024*1024
    alert('Please select image size less than 2 MB');
    return false;
 }
片段

$(文档).ready(函数(){
var URL=window.URL | | window.webkitURL;
$(“#徽标”)。更改(功能(e){
var file=this.files[0],img;
如果(Math.round(file.size/(1024*1024))>2{//以MB为单位,除以1024*1024
警报('请选择小于2 MB的图像大小');
返回false;
}
如果(文件){
img=新图像();
img.onload=函数(){
$('.submit btn').prop('disabled',false);
$(“.error_line”).fadeOut();
};
img.onerror=函数(){
$('.submit btn').prop('disabled',true);
$(“.error_line”).fadeIn();
};
img.src=\u URL.createObjectURL(文件);
}
});
});

仅图像

试试这个

 if(Math.round(file.size/(1024*1024)) > 2){ // make it in MB so divide by 1024*1024
    alert('Please select image size less than 2 MB');
    return false;
 }
片段

$(文档).ready(函数(){
var URL=window.URL | | window.webkitURL;
$(“#徽标”)。更改(功能(e){
var file=this.files[0],img;
如果(Math.round(file.size/(1024*1024))>2{//以MB为单位,除以1024*1024
警报('请选择小于2 MB的图像大小');
返回false;
}
如果(文件){
img=新图像();
img.onload=函数(){
$('.submit btn').prop('disabled',false);
$(“.error_line”).fadeOut();
};
img.onerror=函数(){
$('.submit btn').prop('disabled',true);
$(“.error_line”).fadeIn();
};
img.src=\u URL.createObjectURL(文件);
}
});
});

仅图像


希望这对您有所帮助。到目前为止,答案可能重复,
Image
webapi没有帮助。文件大小不是图片独有的,而
文件
API起到了帮助作用。希望这对您有所帮助。到目前为止,答案可能重复,
Image
webapi没有帮助。文件大小不是图片独有的,而
文件
API起到了帮助作用。
 if(Math.round(file.size/(1024*1024)) > 2){ // make it in MB so divide by 1024*1024
    alert('Please select image size less than 2 MB');
    return false;
 }
 $(document).ready(function() {       
    $('#logo').bind('change', function() {
        var a=(this.files[0].size);
        alert(a);
        if(a > 2000000) {
            alert('large');
        };
    });
});