0

Let me start by saying that I'm not a jQuery guru by any means and I genuinely know this is over my head, that's why I've come to SO.

Is there a way with jQuery to find the file size of a link on a page and then inject/add the text of the file size next to the link?

Here's my problem

On one of my pages, I have a link to my resume which is a PDF file and to improve usability it's proper to have the file type and file size next to the link so the users have the option to decide if they want to click on that link or not. So the link would read something like "Download my resume (PDF / 80KB)"

The problem is that I'm constantly updating my resume and uploading a new PDF file which, of course, has a different file size so I'm always going back to the HTML and changing the text to reflect the new file size.

Is there a way to automate this with jQuery... or plain JavaScript for that matter?

I found this script and made a demo here in Codepen but it doesn't seem to work.

Any help with this would be greatly appreciated.

Ricardo Zea
  • 10,053
  • 13
  • 76
  • 79

2 Answers2

2

you wrote (twice)

json.headers['Content-Length']

when the name of the property (you can see it in the body of the response) is actually in lower case:

json.headers['content-length']

if you correct this key, the output is (pdf 101.6 KB)

Also note that if both your CV and download page are on the same domain, you could simply use a HEAD ajax request and read the content-length response header

Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177
  • This worked great Fabrizio!. One quick question: I see the file type and file size are 'floating'. Is there a way to encapsulate them in a `` tag for CSS targeting purposes? – Ricardo Zea Jun 24 '13 at 16:43
  • Hello everyone. The script is not working anymore since apparently the URL `http://json-head.appspot.com` is returning a 503 response. Does anyone know of a way to get a hold of the main script being served by this site so the call can be made to a local file instead? Thanks again. – Ricardo Zea Oct 03 '13 at 02:45
0

If the size is going to be around 80KB, then it isn't going to be a problem. It seems rather out-dated to put the size in the link. The easy way to do it, is work out a theoretical maximum, e.g. 150KB, which it'll never go over, and put "Download my resume (PDF / < 150KB)"

Other than that, I would have thought that the only way to do this client-side would be to download the file, work out the size, and update the link, which kind of defeats the object.

harriyott
  • 10,505
  • 10
  • 64
  • 103
  • It's not out-dated at all, you just want to do it in differently, which is better than not doing anything at all. "Choosing" 80KB is an arbitrary decision that doesn't take into account the users' right to decide. Maybe for you 80KB is not a big deal but you never know for who it will. So it's a much better user experience to be able to read the **exact** file size than an 'estimated' range. Thanks. – Ricardo Zea Jun 24 '13 at 16:47