`
jinyanhui2008
  • 浏览: 321164 次
  • 性别: Icon_minigender_1
  • 来自: 青岛
社区版块
存档分类
最新评论

js实现计算上传图片大小,并预览效果

阅读更多
<script language="JavaScript" type="text/JavaScript">
<!-- 
var flag=false; 
function DrawImage(ImgD){ 
var image=new Image(); 
image.src=ImgD.src; 
if(image.width>0 && image.height>0){ 
flag=true; 
if(image.width/image.height>= 300/200){ 
if(image.width>300){ 
ImgD.width=300; 
ImgD.height=(image.height*300)/image.width; 
}else{ 
ImgD.width=image.width; 
ImgD.height=image.height; 
} 
ImgD.alt=image.width+"×"+image.height; 
} 
else{ 
if(image.height>200){ 
ImgD.height=200; 
ImgD.width=(image.width*200)/image.height; 
}else{ 
ImgD.width=image.width; 
ImgD.height=image.height; 
} 
ImgD.alt=image.width+"×"+image.height; 
} 
} 
} 
//--> 

function changesrc(){
 document.yourpic.src = document.getElementsByName("filename")[0].value;
 DrawImage(yourpic);

 var   img   =   new   Image();    
    img.src   =   document.yourpic.src;
 alert("fileSize = "+ img.fileSize/1024 +" KB");  
}
</script>
<table width="100%" border="0">
      <tr>
       <td>
       <div align="center"><img border="1" name="yourpic" onload="javascript:DrawImage(this);">
       </div>
       </td>
      </tr>
      <form name="picForm" action="#" method="post" enctype="multipart/form-data" onsubmit="return checkPic(picForm);">
      <tr>
       <td>
       <div align="center"><input type="file" name="filename" onchange="changesrc()"></div>
       </td>
      </tr>
      <tr>
       <td>
       <div align="center"><input type="submit" value="上传照片" /></div>
       </td>
      </tr>
      </form>
      </table>

 

3
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics