图片上传大小,类型,宽高验证

谷歌浏览器测试通过

form表单

<form id="satmpdefForm" action="http://localhost:8080/mainWeb/system/saveStampdefByForm/" style="padding: 10px 20px 10px 40px;"
             enctype="multipart/form-data" method="post">
            <table>
                <tr>
                    <td>标题:</td>
                    <td><input type="text" name="sTitle" id="sTitle"
                        class="easyui-textbox" style="width: 250px;"></td>
                </tr>

                <tr>
                    <td>使用者:</td>
                    <td><input type="text" name="userID" id="userID"
                        class="easyui-textbox" style="width: 250px;"></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input name="imgFile" id="imgFile" type="file"
                        style="width: 100%" accept="image/png,image/jpeg,image/gif" onchange="fileChange(this)"></td>
                </tr>
            </table>
        </form>

javaScript代码,验证图片的大小

function fileChange(target) {
           var fileSize = 0;
           var isIE = /msie/i.test(navigator.userAgent) && !window.opera;//判断是否为IE浏览器
           var Max_Width = 280; //100px
           var Max_Height = 280; //200px
           if(isIE && !target.files) {
             var filePath = target.value; 
             var fileSystem = new ActiveXObject("Scripting.FileSystemObject");        
             var file = fileSystem.GetFile (filePath);     
             fileSize = file.size;    
           }else {    
            fileSize = target.files[0].size;     
            }   
            var size = fileSize / 1024;    
            if(size>2000){  
             showError("附件不能大于2M");
             target.value="";
             return;
            }
            checkImgPX(target,Max_Width,Max_Height);
          }

checkImgPX是验证图片的宽高

/* 
     * 判断图片大小 
     *  
     * @param ths  
     *          type="file"的javascript对象 
     * @param width 
     *          需要符合的宽  
     * @param height 
     *          需要符合的高 
     * @return true-符合要求,false-不符合 
     */ 
    function checkImgPX(ths, width, height) {  
        var img = null;  
        img = document.createElement("img");  
        document.body.insertAdjacentElement("beforeEnd", img); // firefox不行  
        img.style.visibility = "hidden";   
        img.src = ths.value;  
        var imgwidth = img.offsetWidth;  
        var imgheight = img.offsetHeight;  

        //showError(imgwidth + "," + imgheight);  

        if(imgwidth > width || imgheight > height) {  
            showError("图的尺寸应该小于" + width + "x"+ height);  
            ths.value = "";  
            return;  
        }  

    }  

另附验证图片类型的代码

/* 
 * 判断图片类型 
 *  
 * @param ths  
 *          type="file"的javascript对象 
 * @return true-符合要求,false-不符合 
 */ 
function checkImgType(ths){  
    if (ths.value == "") {  
        alert("请上传图片");  
        return false;  
    } else {  
        if (!/\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(ths.value)) {  
            alert("图片类型必须是.gif,jpeg,jpg,png中的一种");  
            ths.value = "";  
            return false;  
        }  

         else
        {
             var img=new Image(); 
             img.src=filepath;   
          while(true){ 
                 if(img.fileSize>0){ 
                 if(img.fileSize>10*1024){       
                     alert("图片不大于10M。"); 
                      return false; 
                      } 
                      break; 
                     } 

                  }
          }
    }  
    return true;  
}  
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值