Getting a file size from a server before downloading it

You must Login before you can answer or comment on any questions.

1 Answer

This is not always supported by the webserver, but the HTTP method "HEAD" can be used for this purpose. An example on how to use it (untested, but should work):

var xhr = Titanium.Network.createHTTPClient();
  xhr.onload = function() {
    Ti.API.info( this.getResponseHeader('Content-Length') );
  };
 
  xhr.open('HEAD', url);
  xhr.send();
So send a HEAD request of the download URL, if you get the Content-Length in the response headers you know the size, else you can display a more generic/animated progress indicator.

(You can try Ti.API.info( this.getResponseHeaders() ); instead for more header variables.)

Your Answer

Think you can help? Login to answer this question!