简体   繁体   中英

How to get image size in bytes using javascript

please tell me how to get image file size in bytes using javascript.

Thanks

If javascript engine supports canvas elements you can try to use canvas element and getImageData to fetch the pixel data from your image. Then, depending on type of the image you could create the binary representation of this image.

Here is info about canvas element and getImagedata api:

http://www.whatwg.org/specs/web-apps/current-work/#dom-context-2d-getimagedata

如果你有一个base64编码的图像src,你可以使用img.src.length * 0.75来确定如果保存到磁盘将采取非常接近的文件大小。

To get the image size, you need to access it on the server. Javascript is a client-side utility, so it can't directly retrieve information from a server.

You'd have to send an Ajax request to communicate with the server. Alternatively, when your page is created, save file sizes in <input type='hidden' /> boxes and access them when you need them, or a similar solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM