用HTML的File控件上传图片,用JS判断和限制图片格式、类型、尺寸

 

//判断图片类型
        var f=document.getElementById("File1").value;
        if(f=="")
        { alert("请上传图片");return false;}
        else
        {
        if(!/\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(f))
        {
          alert("图片类型必须是.gif,jpeg,jpg,png中的一种")
          return false;
        }
        }


 

function CheckFile(f,p)
{
//判断图片尺寸
var img=null;
img=document.createElement("img");
document.body.insertAdjacentElement("beforeend",img);
img.style.visibility="hidden"; 
img.src=f;
var imgwidth=img.offsetWidth;
var imgheight=img.offsetHeight;
if(p.name=="UpFile_Photo1")
{
if(imgwidth!=68||imgheight!=68)
{
alert("小图的尺寸应该是68x68");
}
}
if(p.name=="UpFile_Photo2")
{
if(imgwidth!=257||imgheight!=351)
{
alert("中图的尺寸应该是257x351");
}
}
if(p.name=="UpFile_Photo3")
{
if(imgwidth!=800||imgheight!=800)
{
alert("大图的尺寸应该是800x800");
}
}
//判断图片类型
if(!/\.(gif|jpg|jpeg|bmp)$/.test(f))
{
alert("图片类型必须是.gif,jpeg,jpg,bmp中的一种")
return false;
}
return true;
}


 

<input type="file" id="UpFile_Photo1" runat="server" name="UpFile_Photo1" 
size="35" onpropertychange="CheckFile(this.value,this)">小图<br />
<input type="file" id="UpFile_Photo2" runat="server" name="UpFile_Photo2" 
size="35" onpropertychange="CheckFile(this.value,this)">中图<br />
<input type="file" id="UpFile_Photo3" runat="server" name="UpFile_Photo3" 
size="35" onpropertychange="CheckFile(this.value,this)">大图<br />


 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值