今天整理了一段代码,用于图片上传前预览,并获取图片的长、宽、及字节数,可以用于图片上传前的校验。
脚本如下:
<
html
>
< head >
< script language ="javascript" type ="text/javascript" >
function getimginfo()
{
var img = new Image();
img.src = document.all.file.value;
var sInfo = " Width: " + img.width +
" px Height: " + img.height +
" px Size: " + getsizestring(img.fileSize);
alert(sInfo);
}
function getsizestring(len)
{
var kb = len / 1024;
if (kb < 1024 )
return kb.toFixed( 2 ) + " K " ;
else
{
var m = kb / 1024;
return m.toFixed( 2 ) + " M " ;
}
}
</ script >
</ head >
< body >
< table width ="90%" border ="0" cellspacing ="0" cellpadding ="0" >
< tr >
< td >
< img width ="60" height ="60" id ="picdemo" >
</ td >
< td >
< input type ="file" id ="file" onchange ="document.all.picdemo.src=this.value" class ="allinput" >
< input type ="submit" name ="Submit" value ="上传" onClick ="getimginfo();" >
</ td >
</ tr >
</ table >
</ body >
</ html >
< head >
< script language ="javascript" type ="text/javascript" >
function getimginfo()
{
var img = new Image();
img.src = document.all.file.value;
var sInfo = " Width: " + img.width +
" px Height: " + img.height +
" px Size: " + getsizestring(img.fileSize);
alert(sInfo);
}
function getsizestring(len)
{
var kb = len / 1024;
if (kb < 1024 )
return kb.toFixed( 2 ) + " K " ;
else
{
var m = kb / 1024;
return m.toFixed( 2 ) + " M " ;
}
}
</ script >
</ head >
< body >
< table width ="90%" border ="0" cellspacing ="0" cellpadding ="0" >
< tr >
< td >
< img width ="60" height ="60" id ="picdemo" >
</ td >
< td >
< input type ="file" id ="file" onchange ="document.all.picdemo.src=this.value" class ="allinput" >
< input type ="submit" name ="Submit" value ="上传" onClick ="getimginfo();" >
</ td >
</ tr >
</ table >
</ body >
</ html >