我正在尝试下面的代码,以从实时网址获取KB / MB / GB(而不是宽度和高度)中的图片大小。但这不起作用。
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);
});
上面的代码将获取图像尺寸。
对于文件大小:不,您无法使用jQuery或纯JavaScript获得图像大小。唯一的方法是使用ajax从服务器端获取它。
您可以获取图像URL,将其发送到服务或服务器端页面,并使页面或服务返回图像大小。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句