JQUERY驗證上傳文件大小

 function checkImgType(this_){

 

   var filepath=$(this_).val();

   var extStart=filepath.lastIndexOf(".");

 

   var ext=filepath.substring(extStart,filepath.length).toUpperCase();

 

   if(ext!=".PNG"&&ext!=".GIF"&&ext!=".JPG"){

 

  alert("圖片限於png,gif,jpg格式");

  $(this_).focus();

  if ( $.browser.msie) {  //判斷瀏覽器

    this_.select();

             document.execCommand("delete");

  }else{

$(this_).val("");

  }

  return false;

   }

 

   var file_size = 0;          

                if ( $.browser.msie) {      

                   var img=new Image();

    img.src=filepath;   

 

if(img.fileSize > 0){

 

if(img.fileSize>3*1024){   

alert("圖片不大於3MB。");

$(this_).focus();

     this_.select();

                                  document.execCommand("delete");

 return false ;

}

 

}

 

                } else {

 

                    file_size = this_.files[0].size;   

 

                    console.log(file_size/1024/1024 + " MB");     

 

var size = file_size / 1024;  

alert(size);

if(size > 3){  

alert("上傳的文件大小不能超過3M!");  

$(this_).focus();

$(this_).val("");

return false ;

}

}

       return true;

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章