views:

888

answers:

4

Hello everybody,

My iPhone application downloads large files from the internet (videos, for instance).

I'd like to display a progress bar, so I need to get the size of the remote file I'm trying to download (before downloading it, of course).

I've searched, again and again, but nothing is working. Is there any way to do that ?

Thank you by advance.

+1  A: 

You'll need some mechanism for the server to report the size to you.

I would expect most servers to provide the content-length header entry in the response. That should tell you how big the file is. I would assume whatever class you're using to do the download would expose that information to you.

Herms
+2  A: 

If you're using the URL Loading System (NSURLRequest, NSURLConnection, etc.), then look to see if there's a value for the NSURLConnection's expectedContentLength. Note that it might be NSURLResponseUnknownLength.

invalidname
A: 

Duplicate of this.

mcandre
+1  A: 

Ok, I finally get my answer.

The NSURLConnection calls its delegate with

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;

Well, we need to look inside the NSURLResponse, to find the expectedContentLength

- (long long)expectedContentLength

Return Value The receiver’s expected content length, or NSURLResponseUnknownLength if the length can’t be determined.

Terenn