js图片校验
//上传图片 function AjaxFileUpload(url, fileinput) { try { if (fileinput.files.length <= 0) { alert('请选择图片'); return false; } var file = fileinput.files[0]; var re = /^.*?\.(jpg|gif|png)$/i; if (!re.test(file.name)) { alert('未被允许的图片格式'); return false; } if (file.size > 1024 * 1024 * 2) {//2M $(fileinput).val(''); alert("图片文件须小于2M"); return false; } } catch (err) { } try { validateImageSize(url,fileinput); } catch (err) { doUpload(url);//也要上传,走后台验证 } }; function validateImageSize(url,fileinput) { var f = fileinput.files[0]; var reader = new FileReader(); reader.onload = function (e) { var data = e.target.result; //加载图片获取图片真实宽度和高度 var image = new Image(); image.onload = function () { var width = image.width; var height = image.height; if (width < 640 || height < 360) { $(fileinput).val(''); alert("尺寸须大于640*360像素!"); return; } if (width > 5000 || height > 5000) { $(fileinput).val(''); alert("尺寸须小于5000*5000像素!"); return; } doUpload(url); }; image.src = data; }; reader.readAsDataURL(f); } function doUpload(url) { $.ajaxFileUpload({ url: url, secureuri: false, fileElementId: 'UploadFile', dataType: 'json', beforeSend: function () { $("#loading").show(); }, complete: function () { $("#loading").hide(); }, success: function (data, status) { if (data.Result && data.Result.toLowerCase() == "true") { $("#showImg").attr("src", data.Message); $("#ImageUrl").val(data.Message); } else alert(data.Message); }, error: function (data, status, e) { alert(e); } }); }