获取URL Objective-C的文件大小

时间:2014-09-04 06:59:24

标签: file size nsurl

我正在寻找一种方法来获取NSURL地址的特定文件的文件大小。这就是我尝试过但它总是会回归' 0' 0在日志输出中:

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);

1 个答案:

答案 0 :(得分:4)

在搜索了一些后,我发现了这个解决方案。它在服务器上找到文件的文件大小,并在日志中显示结果。

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);