JS 客户端验证文件类型,获取图片信息

本文介绍了一个使用 JavaScript 实现的图片类型和大小判断的方法。通过读取文件扩展名来判断是否为图片,并获取图片的尺寸和文件大小。
None.gif<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
None.gif
<html xmlns="http://www.w3.org/1999/xhtml">
None.gif
<head>
None.gif
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
None.gif
<title>JS 文件大小及类型判断</title>
ExpandedBlockStart.gifContractedBlock.gif
<style>dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif*
{dot.gif}{ font-size:12px;}
None.gif
</style>
ExpandedBlockStart.gifContractedBlock.gif
<script language="javascript">dot.gif
InBlock.gif
<!--
InBlock.gif
function ShowInfo(sUrl)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
var FSO,F,Ext;
InBlock.gif    
var Extlist = ".gif.jpg.bmp";
InBlock.gif    
//FSO = new ActiveXObject("Scripting.FileSystemObject");
InBlock.gif
    //F = FSO.GetFile(sUrl);
InBlock.gif
    Ext = GetExt(sUrl);
InBlock.gif    
InBlock.gif    
if(Extlist.indexOf(Ext)==-1)
InBlock.gif        document.getElementById(
"Info").innerHTML = "扩展名:" + Ext + " 不是图片类型!  原始路径:" + sUrl;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
elsedot.gif{
InBlock.gif        document.getElementById(
"imgs").src = "";
InBlock.gif        document.getElementById(
"imgs").alt = "图片加载中……";
InBlock.gif        document.getElementById(
"imgs").src = sUrl;
InBlock.gif        
var MyImage = new Image();
InBlock.gif        MyImage.src 
= sUrl;
InBlock.gif        document.getElementById(
"Info").innerHTML = "扩展名:" + Ext + " 合法图片类型!  原始路径:" + sUrl + "<br /> 文件大小:" + MyImage.fileSize + " Bytes  尺寸:" + MyImage.width + "*" + MyImage.height;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
function GetExt(sUrl)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif        
var arrList = sUrl.split(".");
InBlock.gif        
return arrList[arrList.length-1];
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
function DrawImage(ImgD,w,h)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
var flag = false;
InBlock.gif    
var MyImage = new Image();
InBlock.gif    MyImage.src 
= ImgD.src;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
if(MyImage.readyState != "complete")dot.gif{
InBlock.gif        
return false//确保图片完全加载
ExpandedSubBlockEnd.gif
    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
if(MyImage.width > 0 && MyImage.height > 0)dot.gif{
InBlock.gif        flag 
= true;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if(MyImage.width / MyImage.height >= w / h)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if(MyImage.width > w)dot.gif{
InBlock.gif                ImgD.width 
= w;
InBlock.gif                ImgD.height 
= (MyImage.height * w) / MyImage.width;
ExpandedSubBlockStart.gifContractedSubBlock.gif            }
elsedot.gif{
InBlock.gif                ImgD.width 
= MyImage.width;
InBlock.gif                ImgD.height 
= MyImage.height;
ExpandedSubBlockEnd.gif            }

InBlock.gif            ImgD.alt 
= "原始尺寸:" + MyImage.width + "x" + MyImage.height;
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
elsedot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if(MyImage.height > h)dot.gif{
InBlock.gif                ImgD.height 
= h;
InBlock.gif                ImgD.width 
= (MyImage.width * h) / MyImage.height;
ExpandedSubBlockStart.gifContractedSubBlock.gif            }
elsedot.gif{
InBlock.gif                ImgD.width 
= MyImage.width;
InBlock.gif                ImgD.height 
= MyImage.height;
ExpandedSubBlockEnd.gif            }

InBlock.gif            ImgD.alt 
= "原始尺寸:" + MyImage.width + "x" + MyImage.height;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif}

InBlock.gif
function zoomimg(img)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
var zoom=parseInt(img.style.zoom,10|| 100;
InBlock.gif    zoom 
+= event.wheelDelta / 24;
InBlock.gif    imgW 
= img.clientWidth*zoom/100;
InBlock.gif    
if (zoom>10) img.style.zoom = zoom + "%";
InBlock.gif
InBlock.gif    
return false;
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif
-->
None.gif
</script>
None.gif
</head>
None.gif
None.gif
<body>
None.gif
<form id="form1" name="form1" enctype="multipart/form-data" method="post" action="">
None.gif
<p>
None.gif    
<input name="ofile" type="file" id="ofile" onchange="ShowInfo(this.value);" />
None.gif
</p>
None.gif
<id="Info"></p>
None.gif
<p><img src="#" id="imgs" width="300" height="300"  onmousewheel="return zoomimg(this)" onload="DrawImage(this,300,300)" /></p>
None.gif
</form>
None.gif
</body>
None.gif
</html>
None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值