如何从URL获取图像大小 [英] How to get image size from URL

查看:110
本文介绍了如何从URL获取图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试下面的代码,以从实时URL获取以KB/MB/GB(而不是宽度和高度)为单位的图片大小.但这不起作用.

I am trying below code to get the image size in KB/MB/GB (not width and height) from live URL. But it's not working.

var xhr = new XMLHttpRequest();

xhr.open('HEAD', 'http://www.2015autoblog.com/wp-content/uploads/2014/09/2015-Toyota-Land-Cruiser-Front.jpg', true);

调试器

alert(xhr.status);

if ( xhr.status == 200 ) {
    alert('Size in bytes: ' + xhr.getResponseHeader('Content-Length'));
} else {
    alert('ERROR');
}

推荐答案

尝试使用此代码即可

var tmpImg = new Image();
tmpImg.src="https://www.google.com/intl/en_com/images/srpr/logo3w.png"; //or  document.images[i].src;
$(tmpImg).one('load',function(){
  orgWidth = tmpImg.width;
  orgHeight = tmpImg.height;
  alert(orgWidth+"x"+orgHeight);
});

上面的代码将获取图像尺寸.

The above code will gets the image dimension.

对于文件大小: 不,您无法使用jQuery或纯JavaScript获得图像大小.唯一的方法是使用ajax从服务器端获取它.

For the file size: No, there is no way you can get the image size using jQuery or pure JavaScript. The only way is to get it from server side using ajax.

您可以获取图像URL,并将其发送到服务或服务器端页面,并使该页面或服务返回图像大小.

You can get the image url send it to a service or server side page and have the page or service return the image size.

这篇关于如何从URL获取图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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