How to get the file size given a path?
float fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil] fileSize]; NSLog(@"fileSize= %.2f MB",fileSize/1000000);
why can't get file size in objective c? always return 0
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:
orresourceValuesForKeys: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
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。