简体   繁体   English

如何使用Javascript获取网页上图像的文件大小?

[英]How do you get the file size of an image on the web page with Javascript?

I'd like to be able to get the file size on an image on a webpage.我希望能够获取网页上图像的文件大小。

So let's say I have an image on the page (that has loaded) like this:因此,假设我在页面上有一个图像(已加载),如下所示:

How do I call a function in Javascript (or, even better, jquery) to get the file size (not the dimensions) of the image?如何在 Javascript(或者更好的是 jquery)中调用函数来获取图像的文件大小(而不是尺寸)?

It's important to note that I'm not using any inputs or having users upload the image, there's lots of SO answers on getting image sizes from browse buttons with the file API.重要的是要注意,我没有使用任何输入或让用户上传图像,有很多关于使用文件 API 从浏览按钮获取图像大小的 SO 答案。

All I want to do is get the file size of any arbitrary image on the page based of it's id and src url.我想要做的就是根据其 id 和 src url 获取页面上任意图像的文件大小。

Edit: I'm dealing with a keep-alive connection for some images so the Content-Length headers are not available.编辑:我正在处理某些图像的保持活动连接,因此 Content-Length 标头不可用。

You can't directly get the file size (or any data from it).您无法直接获取文件大小(或其中的任何数据)。

The only way is a bit dirty, because you have to do a XMLHTTPRequest (and it probably won't work with externals images, according to the "Cross Origin Resource Sharing").唯一的方法有点脏,因为您必须执行 XMLHTTPRequest(根据“跨源资源共享”,它可能不适用于外部图像)。 But with the browser's cache, it should not cause another HTTP request.但是有了浏览器的缓存,它不应该引起另一个 HTTP 请求。

var xhr = new XMLHttpRequest();
xhr.open("GET", "foo.png", true);
xhr.responseType = "arraybuffer";
xhr.onreadystatechange = function() {
    if(this.readyState == this.DONE) {
        alert("Image size = " + this.response.byteLength + " bytes.");
    }
};
xhr.send(null);

Here is a React version that worked for me, posting it in the event that it could save some time.这是一个对我有用的 React 版本,如果可以节省一些时间,请将其发布。

Form field表单域

<input type="file" 
   name="file"  
   id="file" 
   multiple="multiple" 
   onChange={onChangeHandler} 
   className="fileUp" />

JSX JSX

const onChangeHandler = event => {
    console.log(event.target.files[0].size);
}

Old question but here is my take: I had the same issue but didn't want te rely on buffering to prevent my app from downloading the images twice (as the accepted answer does).老问题,但这是我的看法:我遇到了同样的问题,但不想依靠缓冲来防止我的应用程序下载图像两次(正如接受的答案那样)。 What I ended up doing was fetch the file as a blob, read the file size, and use the blob as image source from there on.我最终做的是将文件作为 blob 获取,读取文件大小,然后使用 blob 作为图像源。 this was easily done with the URL.createObjectURL() function like so:这可以通过URL.createObjectURL()函数轻松完成,如下所示:

let img = new Image()
img.crossOrigin = "Anonymous"; // Helps with some cross-origin issues

fetch('foo.png')
.then(response => response.blob())
.then(blob => { img.src = URL.createObjectURL(blob); })

This looks a lot cleaner and perhaps it can still be useful for some.这看起来更干净,也许它仍然对某些人有用。

Assuming you could use HTML5假设您可以使用 HTML5

  1. Create a canvas创建画布
  2. Put image in there把图像放在那里
  3. Grab the image data with .toDataURLHD()使用.toDataURLHD()获取图像数据
  4. Grab the base64 encoded string抓取 base64 编码的字符串
  5. Use some mad calculation to get the image size in bytes.使用一些疯狂的计算来获得以字节为单位的图像大小。

Have fun!玩得开心!

Maybe it's a lot easier to use a server side script, so it won't be HTML5-dependant也许使用服务器端脚本要容易得多,所以它不会依赖于 HTML5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM