Sunday, February 3, 2013

Find size of file behind download link with jquery before download from server

You could make a HTTP HEAD request, and get a file size approximate by reading the Content-Length HTTP Header.
This kind of request is used to obtain meta-information about the URL implied by the request, without transferring any content of it in the response.
var xhr = $.ajax({
  type: "HEAD",
  url: "http://domain.com/file.ext",
  success: function(msg){
    alert(xhr.getResponseHeader('Content-Length') + ' bytes');
  }
});

No comments:

Post a Comment