gpt4 book ai didi

jquery - 如何从URL获取图片大小

转载 作者:行者123 更新时间:2023-12-03 22:56:45 24 4
gpt4 key购买 nike

我正在尝试使用下面的代码从实时 URL 获取图像大小(以 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,将其发送到服务或服务器端页面,并让页面或服务返回图像大小。

关于jquery - 如何从URL获取图片大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26420420/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com