How to get file size of file from iPhone documents folder

丶灬走出姿态 提交于 2019-12-05 11:08:07

Try this its help you

 NSDictionary *fileDictionary = [[NSFileManager defaultManager] fileAttributesAtPath:videopath traverseLink:YES];
     fileSize = [fileDictionary fileSize];
Hardik Thakkar

Note: Above Method is deprecated so, use below method

Objective-C:

    NSError* error;
    NSDictionary *fileDictionary = [[NSFileManager defaultManager] attributesOfItemAtPath:mediaURL error: &error];
    NSNumber *size = [fileDictionary objectForKey:NSFileSize]; 

Swift:

do
{
   let fileDictionary = try FileManager.default.attributesOfItem(atPath: urlString)
   let fileSize = fileDictionary[FileAttributeKey.size]
   print ("\(fileSize)")
} 
catch{}

** this file size in bytes

Use NSFileManager attributesOfItemAtPath:error

https://developer.apple.com/library/ios/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html#//apple_ref/occ/instm/NSFileManager/attributesOfItemAtPath:error:

It returns an NSDictionary of file attributes and one key it contains is NSFileSize, the size of the file.

try this way.

    // Get file size
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *filePath = [documentsDirectory stringByAppendingString:[directoryContent objectAtIndex:indexPath.row]];
NSDictionary *fileAttributes = [fileManager fileAttributesAtPath:filePath traverseLink:YES];
if(fileAttributes != nil)
{
NSString *fileSize = [fileAttributes objectForKey:NSFileSize];
[[cell detailTextLabel] setText:[NSString stringWithFormat:@"%@ kb", fileSize]];
NSLog(@"File size: %@ kb", fileSize);
}
Geekoder

Before few days I was also searching for something very similar. I was needed to upload the local file to the dropbox and here is something that worked for me: Check the below link:

https://stackoverflow.com/a/1694928/2098401

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!