Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Getting a file size from a server before downloading it

Is there a way to get the file size of say a zip file before it gets downloaded. As detailed in this post https://developer.appcelerator.com/question/26001/copying-a-file-from-a-server-to-your-harddisk, I can download the file. I would like to give my application like a status bar or something but in order to do this I need the file size. I tried using header information but I can't seem to access it until after it is done downloading.

— asked May 24th 2010 by Tyler Johnson
  • desktop
  • download
  • file
  • http
  • httpclient
  • server
  • size
  • zip
0 Comments

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.)

    — answered June 16th 2011 by Julien Nebbout
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.