0

我有一个使用 NSURLConnection 从 url 下载文件的应用程序。我想什么时候点击按钮 progressView 开始下载。在 progressView 顶部是 UILable,它显示状态下载。我想在 UILable 中显示(“0000 MB 下载/1000 MB”)。第一部分是下载的文件量,第二部分是文件大小。我不知道如何显示它。请指导我并告诉我如何显示(下载的数量/大小文件)与 progressView 一起使用。

谢谢。

4

2 回答 2

4

使用NSURLConnection's委托方法:

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
  [responseData appendData:data];
  NSNumber* curLength = [NSNumber numberWithLong:[responseData length] ];
  NSNumber*filesize = [NSNumber numberWithLong: [response expectedContentLength]
  float progress = [curLength floatValue] / [filesize floatValue] ;
  NSLog(@"progress : %f",progress);
}
于 2013-05-08T11:55:27.910 回答
0

如果使用AFNetworking

这里progress是显示下载进度的UIProgressbar

 progress.progress = 0.0;

    currentURL=@"http://www.selab.isti.cnr.it/ws-mate/example.pdf";


    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:currentURL]];
    AFURLConnectionOperation *operation =   [[AFHTTPRequestOperation alloc] initWithRequest:request];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"MY_FILENAME_WITH_EXTENTION.pdf"];
    operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:NO];

    [operation setDownloadProgressBlock:^(NSInteger bytesRead, NSInteger totalBytesRead, NSInteger totalBytesExpectedToRead) {
        progress.progress = (float)totalBytesRead / totalBytesExpectedToRead;

    }];

    [operation setCompletionBlock:^{
        NSLog(@"downloadComplete!");

    }];
    [operation start];
于 2013-05-08T11:56:53.643 回答