4

I'm trying to view the progress bar while downloading a file. The file is generated through PHP, I'm sending the "Content-length" header, which actually works.

The file is downloaded OK so that's not the problem. Unfortunately I just can't get the file size in order to properly display the progress bar.

Here is my code:

write_to_filename = [issue objectForKeyedSubscript:filename];
NSString *post =[[NSString alloc] initWithFormat:@"user_id=%@&email=%@&password=%@",[userData stringForKey:@"userId"],[userData stringForKey:@"email"],[userData stringForKey:@"password"]];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://MY_API_REQUEST"]];

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

[request setValue:@"application/pdf" forHTTPHeaderField:@"Accept"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];
[NSURLConnection connectionWithRequest:request delegate:self];

[issueArray writeToFile:[self saveFilePath] atomically:YES];

The NSURLConnection

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse 

*)response {

    NSLog(@"%lld",[response expectedContentLength]);
    _responseData = [[NSMutableData alloc] init];

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [_responseData appendData:data];
}

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
                  willCacheResponse:(NSCachedURLResponse*)cachedResponse {

    return nil;
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *fileName = [[paths objectAtIndex:0] stringByAppendingPathComponent:write_to_filename];
    [_responseData writeToFile:fileName atomically:YES];

    write_to_filename = nil;
    _responseData = nil;

    [[self IssuesOverviewCollection] reloadData];

}

I've tried many different things, unfortunately it's not working and keeps return -1.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Look at the request/response with a network analyzer such as Charles Proxy. Then you can see what, if anything, is being returned for Content-Length. It may be that the server is not returning Content-Length. – zaph May 23 '14 at 17:36

1 Answers1

9

This can happen if your server is employing compression (which it can do transparently). You can turn off compression on the server by changing your request Accept-Encoding parameter:

[request setValue:@"identity" forHTTPHeaderField:@"Accept-Encoding"];
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Others [have reported](http://stackoverflow.com/a/17444913/1271826) that you can use gzip;q=0, though that didn't work for me. Others [have reported](http://stackoverflow.com/a/11204338/1271826) setting it to an empty string, which does work for me, much like using `identity`, above, did. – Rob May 23 '14 at 19:31