iphone - 如何在下载之前获取文档大小

标签 iphone ios cocoa-touch nsurlconnection

我想下载一个文件,我通过下面的代码来完成

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/

相关文章:

iphone - 为什么 NSDate 不能使用 < 或 > 进行比较?

ios - 如何基于平移手势放大/缩小 UIView?

ios - 如何比较同一个 Swift 枚举的不同实例?

iphone 开发 : communicating with server and push to anotherview

iphone - 射线-三角形交叉点拾取不起作用

ios - 如何将 instantiateViewControllerWithIdentifier 设置为全局变量?

ios - 升级到 Xcode 10 后无法快速识别 Objective C 类

iphone - 如果一个方法在同一时间被调用两次,如何只执行一次?

ios - 添加到 UIButton 上的 UITableView

iphone - 如何隐藏导航栏后退按钮