user1531352
user1531352

Reputation:

Get File Size at URL Objective-C

I'm looking for a way to get the file size of a specific file at an NSURL address. This is what I have tried but it always returns '0' in the log output:

NSString *url = @"http://www.examplesite.com/file01.zip";

unsigned long long fileSize1 = [[[NSFileManager defaultManager] attributesOfItemAtPath:[url lastPathComponent] error:nil] fileSize];
NSLog(@"size of %@ in bytes === %llu", [url lastPathComponent], fileSize1);

Upvotes: 1

Views: 1539

Answers (1)

user1531352
user1531352

Reputation:

After searching some on I found this solution. It finds the file size of the file on the server and shows the result in the log.

NSString *url = @"http://www.examplesite.com/file01.zip";
                NSURL *URL = [NSURL URLWithString:url];
                NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
                [request setHTTPMethod:@"HEAD"];
                NSHTTPURLResponse *response;
                [NSURLConnection sendSynchronousRequest:request returningResponse:&response error: nil];
                long long size = [response expectedContentLength];
                NSLog(@"File size before download: %lld",size);

Upvotes: 5

Related Questions