0

I have one application that downloaded file from url with NSURLConnection. I want when to click download on button progressView starts. in top of progressView is UILable that show me status download. I want show ("0000 MB downloaded /1000 MB") in UILable. first part is downloaded amount of file and second part is size file. I dont know how show it. please guide me about and tell me how show (downloaded amount / size file) that work with progressView.

thanks.

fred
  • 157
  • 2
  • 11
  • Maybe this will help you. http://stackoverflow.com/questions/5027434/progress-bar-downloading-image –  May 08 '13 at 11:51

2 Answers2

4

Use NSURLConnection's delegate method :

-(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);
}
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
0

If using AFNetworking

here progress is the UIProgressbar for showing the download progress

 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];
Lithu T.V
  • 19,955
  • 12
  • 56
  • 101