如何获取远程文件大小以增加进度条?

问题描述:

我需要从网站下载大型pdf,我创建了一个进度条控制器。如何获取远程文件大小以增加进度条?

enter image description here

但是,我怎么设置最大进度值它,我不知道在下载之前的PDF文件大小?
有没有办法获得文件大小,并用它来增加进度条?

我使用

myPDFremoteUrl = "http://www.xasdaxxssxx.pdf"; 
- (float) getFileSize { 
    NSFileManager *man = [[NSFileManager alloc] init]; 
    NSDictionary *attrs = [man attributesOfItemAtPath: myPDFremoteUrl error: NULL]; 
    UInt32 result = [attrs fileSize]; 

    return (float)result; 
} 

,但我不能够以这种方式来检查远程大小...

什么想法?
感谢

我会使用ASIHTTPRequest进行数据检索。它有progress monitoring built in

如果你真的想坚持你所拥有的,你可以看看HTTP头。它有一个名为“Content-Length”的属性可以帮助你。

祝你好运。

+0

很酷。我喜欢这一个! – elp 2011-01-27 09:12:12

+1

ASIHTTPRequest在iOS5中无法正常工作,开发人员不再维护该项目。我建议你在别处寻找这个功能,例如CFNetworking。 – BBonifield 2011-11-09 00:43:05

ASIHTTPRequestdownloadProgressDelegate可以处理此为你 - 它非常容易使用。

已解决。

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    dimensione = (float)[response expectedContentLength]; 

    NSLog(@"content-length: %f bytes", dimensione); 
} 

我忘了didReceiveResponse

不错。