62,046
社区成员
发帖
与我相关
我的任务
分享
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>#</title>
<script type="text/javascript" src="/js/libs/jquery18.js"></script>
<script language="javascript">
<!--
function Checkgo(obj) {
//图片验证
photo_flag = true;
var ff = $("#strPhoto").val();
if(ff == null || ff == ""){
alert("*提示:请选择要上传的照片文件。");
return false;
}else if(!/.(jpg|jpeg)$/.test(ff)){
alert("*提示:照片文件格式必须为jpg格式。");
return false;
}
//设置限制图像的大小为1MB,这里你可以自己设置
var fSize = 1024 * 1024 *1;
//限制图片宽高
var fHeight = 160;
var fWidth = 120;
var fileType;
var fileSize;
var filePath;
var img = new Image();
//显示图片
if (obj.files) {
var reader = new FileReader();
var thisFile = obj.files[0];
fileType = thisFile.type;
fileSize=thisFile.size;
reader.readAsDataURL(thisFile);
//文件读取成功完成时触发
reader.onloadend = function(event){
img.src = event.target.result;
var photo_flag=true;
img.onload = function(event) {
//校验图片尺寸
if(img.width != fWidth || img.height != fHeight){
photo_flag=false;
$("#imgShow").attr("src", "temp.jpg");
alert("*提示:照片尺寸不对。您只能上传"+fWidth+"*"+fHeight+"像素(宽*高)的照片。111");
alert(photo_flag+"111");
//////为何下面return false无法终止提交form提交?
return false;
}
filePath = this.src;
}
//////为何测试时先执行下面的alert?
alert(photo_flag+"222");
}
}else{// 如果是ie浏览器
obj.select();
var path = document.selection.createRange().text;
img.src = path;
if(img.width != fWidth || img.height != fHeight){
photo_flag=false;
$("#imgShow").attr("src", "temp.jpg");
alert("*提示:照片尺寸不对。您只能上传"+fWidth+"*"+fHeight+"像素(宽*高)的照片。");
return false;
}
alert(photo_flag+"333");
var fileType = path.substring(path.length-4,path.length);
if(img.readyState == "complete") {
fileSize = img.fileSize;
}else{
img.onreadystatechange = function(){
// 当图片load完毕
if(img.readyState=='complete'){
fileSize = img.fileSize;
if(fileSize > fSize){
photo_flag = false;
$("#imgShow").attr("src", "temp.jpg");
alert("*提示:照片文件大小应小于1M。");
return false;
}
}
}
}
if (path) {
filePath = path;
}
}
if(
fileType != ".jpg" && fileType != ".JPG" && fileType != "image/jpeg"
){
photo_flag = false;
$("#imgShow").attr("src", "temp.jpg");
alert("*提示:照片文件格式必须是jpg格式。");
return false;
}
//效验文件大小
if(fileSize > fSize){
photo_flag = false;
$("#imgShow").attr("src", "temp.jpg");
alert("*提示:照片文件大小超过了1M。");
return false;
}
if(photo_flag){
$("#imgShow").attr("src", filePath);
} else {
photo_flag = false;
$("#imgShow").attr("src", "temp.jpg");
alert("*提示:照片文件必须为120*160像素、小于1M的jpg格式文件。");
return false;
}
cont_modi.submit();
}
//-->
</script>
</head>
<body>
<form name="cont_modi" action="next.aspx" method="post" onsubmit="return Checkgo(this.strPhoto)">
<input type="file" name="strPhoto" id="strPhoto" accept="image/jpeg,image/jpg" placeholder="请选择照片文件" size="50" />
<input type="submit" value="上传照片" />
</form>
<div><img src="" id="imgShow" style="width:100px;height:100px"></img></div>
</body>
</html>
reader.onloadend = function(event){
img.src = event.target.result;
var photo_flag=true;
img.onload = function(event) {
//校验图片尺寸
if(img.width != fWidth || img.height != fHeight){
photo_flag=false;
$("#imgShow").attr("src", "temp.jpg");
alert("*提示:照片尺寸不对。您只能上传"+fWidth+"*"+fHeight+"像素(宽*高)的照片。111");
alert(photo_flag+"111");
//////为何下面return false无法终止提交form提交?
return false;
}
filePath = this.src;
}
你目前只是在 reader.onloadend 内部进行了return false;无法影响到外侧执行