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