Reputation: 11
I was wondering if there was a way to use js to find out the file size for a file that i'm linking to a webpage.
I want to make it so that on hovering over the download link, a modal box pops up saying the file size of the file
Upvotes: 1
Views: 841
Reputation: 7973
No, for that to happen with Javascript only the browser would have to download the file first to check its size, which beats the purpose. There is a HEAD
method in HTTP, though it may not be supported easily (either by the browser or the server).
In general, when servers show filesizes, they are pre-calculated (and ideally cached) server-side, so using PHP / node.js / Django / whatever back-end. It is possible to retrieve this information asynchronously using AJAX triggered by mouse hover. However, this is probably much more wasteful (bandwidth-wise, code-wise) than simply sending the filesize attached with any file link – the filesize is just a couple of bytes anyway.
Upvotes: 1