我现在用asp做上传图片,但需要在客户端用js就先判断<input name="pic" type="file" />图片控件中的图片宽高尺寸,在ie6下我己经实现了,如下:
var img= new Image();
img.src = document.getElementById("pic").value;
alert(img.width);
alert(img.width);
alert(img.fileSize/1024);
但在ie7、ie8下面就不行,请做过的,有经验的朋友帮忙啊,十万紧急!多谢了
(网上有的说用img.offsetWidth,我试了好像也不行)
var img= new Image();
img.src = document.getElementById("pic").value;
alert(img.width);
alert(img.width);
alert(img.fileSize/1024);
但在ie7、ie8下面就不行,请做过的,有经验的朋友帮忙啊,十万紧急!多谢了
(网上有的说用img.offsetWidth,我试了好像也不行)
document.getElementById("pic").value得到的可能只是文件名,不带路径的,因此
img.src 就找不到文件的。
兼容所有浏览器的方法目前没有,最好在服务器端进行处理比较保险
这个是预览的,在他的js中有个方法可以得到,兼容6、7、8
这种说法不支持,如果啥都在服务器段判断,岂不等于父亲帮儿子把所有的事都做完,儿子就管享受就是了?我一时也说不上咋解决,不过我可以说个方法可能有用。就是你在JS里打断点到你的Img对象那里,调试然后看那个属性得到的是准确的值
{
var img= new Image();
$(img).attr("src",$("#pic").val());
alert($(img).width());
alert($(img).height());
}
$(document).ready(function(){
})
function a()
{
var img= new Image();
$(img).attr("src",$("#pic").val());
alert($(img).width());
alert($(img).height());
}
</script>
<input name="pic" id="pic" type="file" />
<input id="Button1" type="button" value="查看高宽" onclick="a()"/>
return false;
alert(img.offsetWidth);
alert(img.offsetHeight);
不说取不取到文件框里的VALUE的问题。单说:
img.src = 已取到的值;
alert(img.width);
alert(img.height);
这个的话,应该没问题,而且我在IE8里也实际测试过,如果有问题才怪了。
img.src = document.getElementById("pic").value;
alert(img.width+img.scrollLeft);
alert(img.height+img.scorllTop);
alert(img.fileSize/1024);
我在IE7,8用这样的方法是取不到的,客户那里也一样,始终没办法,难道跟每个人的IE设置有关?alert(img.offsetWidth);这种也试了,不行.还是没找到方法.难道真如孟大哥说的一样..看来我得从后台判断了
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript">
function getImageSize(){
var pic = document.getElementById("pic").value;
var img = new Image();
img.onload = function(){
img.onload = null;
alert(img.width);
};
img.src = pic;
}
</script>
</head><body>
<input type="file" id="pic" />
<input type="button" value="上传" onclick="getImageSize()" />
</body>
</html>