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

iOS

File 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
Stone Preston
42,016 Points

check that your file URL is working correctly. NSLog it and see if its null or not.

Here

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

Stone Preston
Stone Preston
42,016 Points

the fileSize is an unsigned long. try replacing

NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];

with

NSNumber *fileSizeNumber = [NSNumber numberWithUnsignedLong:[fileAttributes objectForKey:NSFileSize]];

2 things, it's warning me with the below message.

  1. incompatible pointer to integer conversion sending 'id' to parameter of type 'unsigned long'
  2. still getting 0 as value.
Stone Preston
Stone Preston
42,016 Points

whats the line of code thats giving you that error?

And I checked "fileAttributes" dictionary values and it's 'nil'.

The line of code you suggested.

Stone Preston
Stone Preston
42,016 Points

hmm 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];

No, it's returning "nil"

I 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
Stone Preston
42,016 Points

ok so your path doesnt exist. How are you getting the URL?

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

       NSString *URL = [((NSURL *)[info objectForKey:UIImagePickerControllerReferenceURL]) path];
 }