1

I know a way to download file using Afnetworking from here.I am using that too.

How to download a file and save it to the documents directory with AFNetworking?

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"..."]];
AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:request] autorelease];

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

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Successfully downloaded file to %@", path);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

[operation start];

Problem is that it also download corrupted file or status not okay file.

Some people ask to check operation.hasAcceptableStatusCode.

How shall I do?

Community
  • 1
  • 1
Khant Thu Linn
  • 5,905
  • 7
  • 52
  • 120
  • Did you checked the other answers? I know you used matt's answer (who's one of the best qualified to tell you how to use AFNetworking), but it's response is quite old, and AFNetworking has a lot changed since then. – Larme Feb 17 '16 at 08:32
  • yes I check. but they never mention how to download inside success block and how to avoid setting output stream. – Khant Thu Linn Feb 17 '16 at 15:22

0 Answers0