Reputation: 68
i am a new to objective c, i tried many hours to find out how to get file size but i failed, please help me, thx a lot.
first i use code like this:
NSString *yourPath = @"http://www.xxx.com.au/magazine/nm.pdf";
NSFileManager *man = [[NSFileManager alloc] init];
NSDictionary *attrs = [man attributesOfItemAtPath: yourPath error: nil];
unsigned long long result = [[attrs objectForKey: NSFileSize] unsignedLongLongValue];
NSLog(@"%llu", result);
output is 0
then i try this:
NSString *yourPath = @"http://www.xxx.com.au/magazine/nm.pdf";
unsigned long long result = [[[NSFileManager defaultManager] attributesOfItemAtPath: yourPath error:nil] fileSize];
NSLog(@"%llu", result);
also get 0. Same result after i change path to local file.
Upvotes: 1
Views: 2223
Reputation: 5316
Your path is not a path, it's an URL with http protocol.
NSFileManager
only works for filesystem files (expects paths, not urls). Also use of NSFileManager
is no more the recommended way to get the size of a file in Cocoa.
The recommended way now is to use NSURL
. But it must be a 'file url' (create it with [NSURL fileURLWithPath:myPath]
). If it is a 'file url' you can use getResourceValue:forKey:error:
or resourceValuesForKeys:error:
to get information like the file size.
In your case, however, you do not have a path, but an http URL. An http URL refers to a resource, not to a file (a resource may or may not be a file). If it's a file there's no way to know its size without at least starting to download it. Depending on how the server responds it may or may not be possible to know the size at the beginning of the download. In the worst case you only know the size after downloading the file completely.
Upvotes: 3