AFNetworking - 保存下载的文件

jwk*_*knz 6 objective-c download ios afnetworking

我正在使用AFNetworking并且可以成功下载文件.

在下载结束时,它不会出现在我设置的目录中.

我做了一些搜索,在SO上发现了一些问题,建议我使用:

[_operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
Run Code Online (Sandbox Code Playgroud)

但是这出现了一个错误,据我所知,在他们的文档中没有提到.

错误是:

/ Users/Jeff/Documents/Dropbox-01/Dropbox/Xcode Projects/Try Outs - JEFF/testDownload/testDownload/JWKDownloadViewController.m:177:10:'AFURLConnectionOperation'没有可见的@interface声明选择器'setCompletionBlockWithSuccess:failure:'

我需要使用更新的行吗?

小智 8

    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];
Run Code Online (Sandbox Code Playgroud)


Par*_*iya 4

是的,请确保您使用了正确的路径NSOutputStream

添加这个:

[_operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Successfully downloaded file");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  NSLog(@"Error: %@", error);
}];
[_operation start];
Run Code Online (Sandbox Code Playgroud)