求助:jsp上传图片时限制长宽

xwylpsy 2009-06-25 10:55:58
jsp上传图片时限制长宽:如:我想设置它上传图片时width不能超过100,hight不能超过100!
...全文
273 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
yxyht 2012-05-11
  • 打赏
  • 举报
回复
小_Y 2009-06-27
  • 打赏
  • 举报
回复
xue xi le.
ddyouyue 2009-06-27
  • 打赏
  • 举报
回复
都在服务器端判断,很耗服务器资源。最好现在客户端判断
fys249931556 2009-06-27
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 qiheia 的回复:]
如果你使用的是jspsmartupload控件上传的话,就有这个方法来控制文件上传的大小
su.setMaxFileSize(10 * 100* 100);
[/Quote]

嗯。。我感觉最好也是在服务器端验证。。页面中是可用禁用js的。

当然客户端和服务器端双重验证最好。
qiheia 2009-06-27
  • 打赏
  • 举报
回复
如果你使用的是jspsmartupload控件上传的话,就有这个方法来控制文件上传的大小
su.setMaxFileSize(10 * 100* 100);
kadach11 2009-06-25
  • 打赏
  • 举报
回复
纠正下代码:
<html>
  <head>
  <title>Upload Image</title>
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<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(){
document.getElementById('pic_show').style.display = '';
  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>
  <div id="pic_show" style="display:none;"><img name="picshow" src="" title="图像预览"></div>
  </body>
  </html>
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 xwylpsy 的回复:]
回1、2楼:不行呀!不会判断其图片大小!
[/Quote]
把1楼2楼的脚本函数换成3楼的,我少打了句话,3楼的函数是正确的
kadach11 2009-06-25
  • 打赏
  • 举报
回复
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>

xwylpsy 2009-06-25
  • 打赏
  • 举报
回复
回1、2楼:不行呀!不会判断其图片大小!
  • 打赏
  • 举报
回复
函数有点错误
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,返回逼供内提示;
}
}
  • 打赏
  • 举报
回复
<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里的数就可以了
  • 打赏
  • 举报
回复
<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里的数就可以了

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧