-1

单击该按钮时我有一个按钮,我需要获取图像文件大小

function attachPhoto()
{
    var path="http://www.w3schools.com/images/pulpit.jpg";// i need to check size of image
}

<input type="button" style="width:30%" onClick=" attachPhoto()" />
4

4 回答 4

0

$('image').width()$('image').height()

只有在它完全加载之后(也许将它附加到.load()事件中)。

于 2012-08-07T14:00:11.730 回答
0

这是一个非 jQuery 解决方案:

function attachPhoto() {
    var img = new Image();
    img.onload = function() {
        alert([img.width, img.height]);
    } 
    img.src = "http://www.w3schools.com/images/pulpit.jpg";
}

请注意,图像大小在文件加载后可用,并且onload回调将被异步调用。如果您希望在知道必须从该回调内部触发的大小后发生其他事情。

于 2012-08-07T14:06:52.730 回答
0

$('element').width() 或者$('element').height()

于 2012-08-07T13:59:24.523 回答
0

如果您想仅使用 URL 获取文件的尺寸,则需要使用 PHP 等外部语言。jQuery 无法检索文件属性。

一种方法是将图像加载到屏幕外的某个位置并获取 $('image').width() & $('image').height() ,但这不是一种理想的方式。

最好的方法是对 php 脚本使用 ajax 调用,该脚本将检查文件的尺寸并将其返回给 jQuery。

您可以访问 php 服务器吗?

于 2012-08-07T14:03:42.070 回答