这个问题在这里已经有了答案:
Best Answer-推荐答案 strong>
使用 NSURLConnection
和 Head
请求,响应将有 expectedContentLength :
请求
NSMutableURLRequest * req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString"http://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png"]];
[req setHTTPMethod"Head"];
NSURLConnection * con = [[NSURLConnection alloc] initWithRequest:req
delegate:self];
[con start];
[con release];
委托(delegate)
- (void)connectionNSURLConnection *)connection didReceiveResponseNSURLResponse *)response
{
NSLog(@"didReceiveResponse (%lld)", response.expectedContentLength);
}
注意它可能是负面的:
#define NSURLResponseUnknownLength ((long long)-1)
对于 NSURLSession
,使用接受 NSURLRequest
对象作为参数的任务工厂方法,例如- (NSURLSessionDataTask *)dataTaskWithRequestNSURLRequest *)request completionHandlervoid (^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler:
关于iphone - 如何使用 url 获取文件大小?,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/15920637/