js圖片上傳驗證圖片格式和大小尺寸

來源:互聯網
上載者:User

標籤:blog   java   檔案   width   2014   io   

<!DOCTYPE html><html><head lang="en">    <meta charset="UTF-8">    <title></title>    <script type="text/javascript">    function getImageSize(obj){var _file=document.getElementById("file");   var i=_file.value.lastIndexOf('.');   var len=_file.value.length;   var extEndName=_file.value.substring(i+1,len);   var extName="GIF,BMP,JPG,JPEG";//首先對格式進行驗證   if(extName.indexOf(extEndName.toUpperCase())==-1){//filemessage.innerHTML="*您只能輸入"+extName+"格式的檔案"alert("*您只能輸入"+extName+"格式的檔案");   }else{     var url,image;    if(obj.files){       if (window.navigator.userAgent.indexOf("MSIE") >= 1) {                obj.select();                url=document.selection.createRange().text;            }            //firefox            else if (window.navigator.userAgent.indexOf("Firefox") >= 1) {               url= window.URL.createObjectURL(obj.files.item(0));            }else{            url= obj.files.item(0).getAsDataURL();            }    }else{       url=obj.value;       url="file:///"+url;    }    image=new Image();    image.src=url;    image.onload=function(){    alert(image.width);    alert(image.height);      if(image.width!=80||image.height!=80){       // filemessage.innerHTML="*提示:檔案大小不對。您只能上傳985*123尺寸的圖片或者swf檔案";       alert("*提示:檔案大小不對。您只能上傳80*80尺寸的圖片");      }    } }}    </script></head><body><input type="file" name="file" id="file" onchange="getImageSize(this)"/></body></html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.