日期:2014-05-17  浏览次数:20659 次

求助:jsp上传图片时限制长宽
jsp上传图片时限制长宽:如:我想设置它上传图片时width不能超过100,hight不能超过100!

------解决方案--------------------
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>文件上传</title>
<script language="javascript">
function yanzheng()
{
var img_1=document.getElementById("img_1");
alert(img_1.width); //图片宽
alert(img_1.height); //图片高
if(img_1.width>100)
{
alert("宽大于100了");
return;//如果宽大于100,返回逼供内提示;
}
}

</script>
</head>

<body>
<img src="" id="img_1" name="img_1" style="visibility:hidden">
<table width="50%" valign="botom" height="22" border="1">
<!--存在两个不同表单所以需要不同的名字-->
<form method="post" name="myform1" enctype="multipart/form-data" >
<tr>
<td><input type="file" name="file1" size="20" /></td>
<td><input type="button" value="上传" onClick="yanzheng()" >
</td>
</tr>
</form>
</table> 
</body>
</html>
要注意的就是弹出的高度和宽度不一定和楼主说的100是同一个单位,你自己测试1下,根据需要设置下if里的数就可以了
------解决方案--------------------
函数有点错误
function yanzheng() 

var img_1=document.getElementById("img_1"); 
img_1.src=myform1.file1.value;//加上这句,获取到的width就是图片的分辨率
alert(img_1.width); //图片宽 
alert(img_1.height); //图片高 
if(img_1.width>100) 

alert("宽大于100了"); 
return;//如果宽大于100,返回逼供内提示; 


 
------解决方案--------------------
LZ试下这个吧,我亲自测试过没问题,CLINT端判断。

<html>
  <head>
  <title>Upload Image</title>
  <SCRIPT LANGUAGE="JavaScript">
  <!--
  function imgExceedSize(w,h){
  if(!document.IUpload.picsrc.value==""){
  if(picshow.width>w||picshow.height>h){
  alert("图像尺寸:"+picshow.width+"X"+picshow.height+"。\\图像尺寸过大!你只能上传尺寸为 "+w+"×"+h+"的图像,请重新浏览图片!");
  return true;
  }else{
  return false;
  }
  }else{
  return true;
  }
  }
  function detect(){
  var ok=imgExceedSize(100,100);
  if(ok){
  document.IUpload.reset();
  }else{
  document.IUpload.submit();
  }
  }
  //-->
  </SCRIPT>
  </head>

  <body>
  <form name="IUpload" action="upfile.asp" method="post">
  <p><input type="file" name="picsrc" size="40" onchange="picshow.src=document.IUpload.picsrc.value">
  <input type="button" value="上载" onclick="detect()"></p></form>
  </body>

  </html>