views:

2073

answers:

5

I was wondering if there was a way to use jQuery to find out the file size for a PDF 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 PDF. I can do the second bit, the only thing i'd like to know is how to find out the file size. I dont know if jQuery is capable of this. If not then too bad i guess..

Cheers in advance.

A: 

If you know the file size beforehand, you can put that in the title attribute of the download link. You can also use jQuery to show a custom popup.

Otherwise, there is no way to get just the file size (content-length) of a URL without actually downloading the whole content AFAIK.

Chetan Sastry
A: 

I am 99% sure that javascript (and therefor jQuery) cannon access file size information. You'd need to upload the file and check it on the server side.

idrumgood
A: 

The only way that I can think of to do this would be to create a web service that would return you the filesize of the PDF.

Corey Sunwold
A: 

You can't use javascript to fetch file-information from the server. So you can't use jQuery for this, you'll have to use some sort of server-side scripting to get the file information and display it.

tj111
+11  A: 

I feel like this is too much to be doing client-side, but I don't know the details of your application, so presuming you have to do it with jQuery... you can do a HEAD request and look at the Content-Length header. Earlier I recommended the jqHead plugin, but after I tried to use it I found that it was broken.

Here's a working example that doesn't depend on any extra plugins: http://host.masto.com/~chris/so/jqhead.html

The meat of it is very simple:

  var request;
  request = $.ajax({
    type: "HEAD",
    url: $("#url").val(),
    success: function () {
      alert("Size is " + request.getResponseHeader("Content-Length"));
    }
  });
masto
I was basically writing an answer suggesting a HEAD request, but you beat me too it. May help to show code to do it with/without the plugin.
eyelidlessness