Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

harishupadhyayula
32,221 PointsFile upload size
I am trying to upload a image/video to my backend and I would like check the file size before uploading it. This is what I have done so far.
-(BOOL)returnFileSize:(NSString *)URL{
NSError *attributesError;
BOOL _isWithInFileSizeLimit = NO;
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:URL error:&attributesError];
NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
TFLog(@"Uploading file size of limit .. %d", (int)fileSizeNumber);
NSLog(@"UPLOAD FILE SIZE... %d", (int)fileSizeNumber);
if ((long)[fileSizeNumber longValue] <= 15) {
_isWithInFileSizeLimit = YES;
}
return _isWithInFileSizeLimit;
}
// calling the above method, which doesn't work. It always returns file size as 0.
NSString *URL = [((NSURL *)[info objectForKey:UIImagePickerControllerReferenceURL]) absoluteString];
if ([self returnFileSize: URL]) {
[self uploadMedia];
}else{
// Display Errir Message;
}
Any suggestions?
9 Answers

Stone Preston
42,016 Pointscheck that your file URL is working correctly. NSLog it and see if its null or not.

harishupadhyayula
32,221 PointsHere
(NSString *) $0 = 0x0c3a9500 @"assets-library://asset/asset.JPG?id=ECB1C3F7-8430-4E01-B29D-9AEDA12056D2&ext=JPG"

Stone Preston
42,016 Pointsthe fileSize is an unsigned long. try replacing
NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
with
NSNumber *fileSizeNumber = [NSNumber numberWithUnsignedLong:[fileAttributes objectForKey:NSFileSize]];

harishupadhyayula
32,221 Points2 things, it's warning me with the below message.
- incompatible pointer to integer conversion sending 'id' to parameter of type 'unsigned long'
- still getting 0 as value.

Stone Preston
42,016 Pointswhats the line of code thats giving you that error?

harishupadhyayula
32,221 PointsAnd I checked "fileAttributes" dictionary values and it's 'nil'.

harishupadhyayula
32,221 PointsThe line of code you suggested.

Stone Preston
42,016 Pointshmm alright. Just found this on stack overflow. It will get you the file size all in one line.
uint64_t fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:URL error:&attributesError] fileSize];

harishupadhyayula
32,221 PointsNo, it's returning "nil"

harishupadhyayula
32,221 PointsI tried to read the "attributesError" and I get this error
Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" UserInfo=0xc737530 {NSFilePath=assets-library://asset/asset.JPG?id=ECB1C3F7-8430-4E01-B29D-9AEDA12056D2&ext=JPG, NSUnderlyingError=0xc749730 "The operation couldn’t be completed. No such file or directory"}

Stone Preston
42,016 Pointsok so your path doesnt exist. How are you getting the URL?

harishupadhyayula
32,221 Points-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSString *URL = [((NSURL *)[info objectForKey:UIImagePickerControllerReferenceURL]) path];
}