I am trying to view a progress bar while downloading a file. The file is created via PHP, I send the header "Content-length", which really works.
The file is scanned OK so that there is no problem. Unfortunately, I just can't get the file size to display the progress bar correctly.
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];
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 tried a lot of different things, unfortunately it does not work and saves return -1.
source
share