views:

67

answers:

1

Any way to get the size of a (remote, http) file without actually downloading it?

All other similar questions seem to revolve around grabbing the expectedContentLength from the NSURLResponse object in didReceiveResponse: but I don't want to download the file, I just want to know how big it is.

+2  A: 

Try to make HEAD request, it returns only headers with Content-Length header included.

~$ curl -I http://google.com/
HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
Content-Length: 219
...
Generally correct, but just in case the server sends Content-Length, it may not do this if the content length is unknown for it. But this is the best client can do.
Artyom
Hm, seems NSURL is buggy with HEAD requests (at least according to http://www.cocoabuilder.com/archive/cocoa/99348-head-request-using-nsurlconnection.html ) but I'll give that a shot. Thanks!
Kalle
Content-Length also reports gzip compressed length, if compression is turned on on the server. Case in point, http://google.com/ is most definitely more than 219 bytes.
Kalle