Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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