在jQuery中上传时限制图像大小

时间:2012-07-11 10:50:46

标签: jquery

我想在上传时限制图片尺寸。是否有任何jQuery插件来检查上传图像的大小?

1 个答案:

答案 0 :(得分:0)

使用此添加jquery库并尝试

 <script type="text/javascript">
 function Size() {
  if ( $.browser.msie ) {
    var a = document.getElementById('file').value;
       $('#myImage').attr('src',a);
       var imgbytes = document.getElementById('Image').fileSize;
       var imgkbytes = Math.round(parseInt(imgbytes)/1024);
       alert(imgkbytes+' KB');
   }else {
       var fileInput = $("#file")[0];
       var imgbytes = fileInput.files[0].fileSize; // Size returned in bytes.
       var imgkbytes = Math.round(parseInt(imgbytes)/1024);
               alert(imgkbytes+' KB');
     }
  }    
  </script>

  <img id="Image" src="" style="display:none;"><br>
  <button onclick="Size();">Image Size</button>
  <input type="file" id="file" />
  <input type="button" value="find size" onclick="Size()" />
相关问题