上传图片获取宽高

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>获取上传图片的宽高</title>
</head>
<body>
  <form>
    <input type="file" name="img_upload" id="img_upload" onchange="imgUpload();" />
  </form>
</body>
<script type="text/javascript">
  function imgUpload() {
    var file = document.getElementById('img_upload');
    createReader(file.files[0], function (w, h) { alert(w + ' '+ h)});
  }
  createReader = function(file, whenReady) {
    var reader = new FileReader;
    reader.onload = function (evt) {
        var image = new Image();
        image.onload = function () {
            var width = this.width;
            var height = this.height;
            if (whenReady) whenReady(width, height);
        };
        image.src = evt.target.result;
    };
    reader.readAsDataURL(file);
  }
</script>
</html>
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页