如何在jquery中检查图像文件大小? [英] How to check the image file size in jquery?

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

问题描述

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

I have a button while clicking that button I need to get the image file size

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()" />


推荐答案

这是一个非jQuery解决方案:

Here's a non-jQuery solution:

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

请注意,图片大小可用加载文件后, onload 回调将被异步调用 。如果你想知道必须从里面那个回调中触发的大小,你想要发生其他事情。

Note that the image size will only be available once the file is loaded, and the onload callback will be called asynchronously. If you want other stuff to happen once you know the size that must be triggered from inside that callback.

这篇关于如何在jquery中检查图像文件大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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