我想下载一个文件,我通过下面的代码来完成
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:str] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:500];
[request setHTTPMethod:@"GET"];
NSData *returnData =[[NSURL alloc]init];
returnData=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
根据下载的数据,我想显示进度条,有什么办法可以做到这一点。
提前致谢。
最佳答案
在你的 NSURLConnection
委托(delegate)中实现方法 - (void)connection:(NSURLConnection *)theConnection didReceiveResponse:(NSURLResponse *)response
。在这个方法中你可以调用
long long expLength = [response expectedContentLength];
在 expLength
中,您将获得预期的文档大小。
但要小心:
Some protocol implementations report the content length as part of the response, but not all protocols guarantee to deliver that amount of data. Clients should be prepared to deal with more or less data.
关于iphone - 如何在下载之前获取文档大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7333911/