FileUpload客户端JS校验图片大小

<asp:FileUpload ID="InchPhotoFile" CssClass="display" onchange="CheckResumeField(this)"
runat="server" />

//上传附件
function CheckResumeField(obj) {
var id = obj.id;
var imgPath = obj.value;
var ua = window.navigator.userAgent;
var filesize = 0;
if (ua.indexOf("Firefox") >= 1 || ua.indexOf("Chrome") >= 1) {
filesize = obj.files[0].size
} else if (ua.indexOf("MSIE") >= 1) {
var img = new Image();
img.src = imgPath;
filesize = img.ownerDocument.fileSize;
} else {
alert("您的浏览器暂不支持计算上传文件的大小,确保上传文件不要超过2M,建议使用IE、FireFox、Chrome浏览器。");
return;
}
imgPath = imgPath.toLowerCase().substr(imgPath.lastIndexOf("."));
//500K
if (id == "InchPhotoFile") {
if (filesize / 1024 > 5 * 1024) {
alert("照片大小不能超过500K!");
return;
}
if (imgPath == ".jpg" || imgPath == ".bmp" || imgPath == ".gif") {
} else {
alert("请上传jpg bpm gif 格式的照片");
return;
}
} else if (id == "ResumeField") {
if (filesize / 1024 > 1 * 1024 * 1024) {
alert("简历大小不能超过3M!");
return;
}
} else if (id == "LifeFile") {
if (filesize / 1024 > 3 * 1024 * 1024) {
alert("生活照大小不能超过3M!");
return;
}
if (imgPath == ".jpg" || imgPath == ".bmp" || imgPath == ".gif") {
} else {
alert("请上传jpg bpm gif 格式的照片");
return;
}
}

var btnId = "btn" + id;
$("#" + btnId).trigger('click'); //触发按钮单击事件
}

原文地址:https://www.cnblogs.com/blue-Sea/p/5109484.html