JS - File Reader API获取图像文件的大小和尺寸 [英] JS - File Reader API get image file size and dimensions

查看:279
本文介绍了JS - File Reader API获取图像文件的大小和尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在使用以下代码使用File Reader API获取上传图片:

Hi i'm using the following code to get the upload image using File Reader API:

<script type="text/javascript">
var loadImageFile = (function () {
    if (window.FileReader) {
        var oPreviewImg = null, oFReader = new window.FileReader(),
            rFilter = /^(?:image\/bmp|image\/cis\-cod|image\/gif|image\/ief|image\/jpeg|image\/jpeg|image\/jpeg|image\/pipeg|image\/png|image\/svg\+xml|image\/tiff|image\/x\-cmu\-raster|image\/x\-cmx|image\/x\-icon|image\/x\-portable\-anymap|image\/x\-portable\-bitmap|image\/x\-portable\-graymap|image\/x\-portable\-pixmap|image\/x\-rgb|image\/x\-xbitmap|image\/x\-xpixmap|image\/x\-xwindowdump)$/i;

        oFReader.onload = function (oFREvent) {

            /*get image*/
            var _img = oFREvent.target.result;
            console.log(oFREvent.target);
            /*add img to hidden input text*/
            localStorage.photo = _img;
            oPreviewImg.src = oFREvent.target.result;
        };

        return function () {
            var aFiles = document.getElementById("imageInput").files;
            if (aFiles.length === 0) { return; }
            if (!rFilter.test(aFiles[0].type)) { 
                notify("You must select a valid image file!",3400,false); return; 
            }
            oFReader.readAsDataURL(aFiles[0]);
        }

    }

})();
</script>

<form name="uploadForm">
<p><input id="imageInput" type="file" name="myPhoto" onchange="loadImageFile();" /><br />
<input type="submit" value="Send" /></p>
<input type="hidden" id="photo_1_hidden" name="photo_1"/>
</form>

它运行良好,它返回图像的base64数据。

it works great and it returns the base64 data of the image.

现在我想得到图像文件大小和图像宽度和高度。

now i would like to get also the image file size and the image width and height.

是否可以?

我尝试登录控制台文件,但我找不到我正在搜索的内容。

I tryed to log in console the file but i can't find what i'm searching for.

任何帮助appriciated感谢所以很多!

Any help appriciated thanks so much!

推荐答案

这样的事情?

var oPreviewImg = new Image();
oPreviewImg.onload = function(){
  console.log(this.size);
  alert("'" + this.name + "' is " + this.width + " by " + this.height + " pixels in size.");
  return true;
};

oPreviewImg.onerror = function(){
  alert("'" + this.name + "' failed to load.");
  return true;
}

oPreviewImg.src = "//placeholdit.imgix.net/~text?txtsize=33&txt=350%C3%97150&w=350&h=150";

var xhr = new XMLHttpRequest();
xhr.open('HEAD', oPreviewImg.src, true);
xhr.onreadystatechange = function() {
    console.log('this', this);
  if ( xhr.readyState == 4 ) {
    if ( xhr.status == 200 ) {
      alert('Size in bytes: ' + xhr.getResponseHeader('Content-Length'));
    } else {
      alert('ERROR');
    }
  }
};
xhr.send(null);

实时版本

更新实时版本替换为Fiddle,但是,到期跨站点脚本问题,不再有效地检索大小。

Update Live version replaced with Fiddle, however, due to cross site scripting concerns, the size is no longer being retrieved effectively.

这篇关于JS - File Reader API获取图像文件的大小和尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆