jsp做网页上传图片时,怎样限制上传图片的尺寸?
怎样限制上传图片的尺寸?就是在图片上传之前,用javascript报错?比如高度有个限制180,宽度不能超过130...
怎样限制上传图片的尺寸?就是在图片上传之前,用javascript报错?
比如高度有个限制180,
宽度不能超过130 展开
比如高度有个限制180,
宽度不能超过130 展开
展开全部
试试如下代码
<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>
<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>
参考资料: csdn
展开全部
前端js判断就是元素的width和length属性
后端
File picture = new File("上传的图片位置");
BufferedImage uploadImg =ImageIO.read(new FileInputStream(picture));
System.out.println(String.format("%.1f",picture.length()/1024.0));
System.out.println(uploadImg.getWidth());
System.out.println(uploadImg.getHeight());
后端
File picture = new File("上传的图片位置");
BufferedImage uploadImg =ImageIO.read(new FileInputStream(picture));
System.out.println(String.format("%.1f",picture.length()/1024.0));
System.out.println(uploadImg.getWidth());
System.out.println(uploadImg.getHeight());
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询