Determining image file size + dimensions via Javascript?

后端 未结 11 1104
执念已碎
执念已碎 2020-11-22 07:00

As part of a web app, once images have been downloaded and rendered on a web page, I need to determine an image\'s file size (kb) and resolution within the browser context (

11条回答
  •  太阳男子
    2020-11-22 07:59

    You can get the dimensions using getElement(...).width and ...height.

    Since JavaScript can't access anything on the local disk for security reasons, you can't examine local files. This is also true for files in the browser's cache.

    You really need a server which can process AJAX requests. On that server, install a service that downloads the image and saves the data stream in a dummy output which just counts the bytes. Note that you can't always rely on the Content-length header field since the image data might be encoded. Otherwise, it would be enough to send a HTTP HEAD request.

提交回复
热议问题