AFNetworking-使用AFHTTPRequestOperation下载文件吗? [英] AFNetworking - Using AFHTTPRequestOperation to download file?

查看:86
本文介绍了AFNetworking-使用AFHTTPRequestOperation下载文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

老兄,被这个困住了。尝试使用 AFNetworking 下载文件,而我能做的最好的就是捕获downloadProgressBlock中的进度字节。我已经尝试了AFNetworking的许多不同分支,现在回到最新版本。好像一次修改了AFHTTPRequestOperation,使其也包含NSURLResponse,但是在最新版本中已经没有了。而且,按照下面的代码,永远不会调用成功块。我得到下载字节的日志,然后调用^ complete。成功和错误永远都不会被召唤。

Man, stumped on this one. Trying to download a file with AFNetworking and the best I can do is capture the progress bytes in the downloadProgressBlock. I've tried many different forks of AFNetworking and now I'm back to the latest build. Looks like at one time the AFHTTPRequestOperation was modified to also include NSURLResponse but that's gone in the latest release. And, per the code below, the "success" block is never called. I get a log of the downloaded bytes and then ^complete is called. success and error are never called.

任何有关此的指导都很棒。我不知道数据返回的位置以及如何在其上使用NSFileManager?我需要下载文件,而不是为图像写流,等等。

Any guidance on this would be awesome. I can't figure out where the data is returned and how to get it to use NSFileManager on it? I need to download files, not write streams for images, etc.

编辑:我也试图通过覆盖-(void)connection:didReceiveData来捕获数据。

I've also tried to capture the data by overriding - (void)connection:didReceiveData as suggested in the documentation but nothing doing there.

//网址为 http://mydomain.com/somezip.zip

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", url]]];

AFHTTPRequestOperation *operation = [AFHTTPRequestOperation HTTPRequestOperationWithRequest:request success:^(id object) {

    NSLog(@"hey, some success!");

} failure:^(NSHTTPURLResponse *response, NSError *error) {

    NSLog(@"%@", error);

}];


[operation setDownloadProgressBlock:^(NSInteger bytesRead, NSInteger totalBytesRead, NSInteger totalBytesExpectedToRead) {

    // callback is sent in and works great.  Just outputting to the console at the moment
    SEL callme = NSSelectorFromString(callback);
    [sender performSelectorOnMainThread:callme withObject:[NSNumber numberWithInt:bytesRead] waitUntilDone:NO];

}];

[operation setCompletionBlock:^{
    NSLog(@"operation complete");
}];

NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
[queue addOperation:operation];


推荐答案

您可以简单地使用AFNetworking附带的此功能甚至保存到磁盘-
注意 operation.outputStream

You can simply use this feature that is included on AFNetworking and even save it to disk - Notice the operation.outputStream:

NSMutableURLRequest* rq = [api requestWithMethod:@"GET" path:@"YOUR/URL/TO/FILE" parameters:nil];
AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:rq] autorelease];

NSString* path=[@"/PATH/TO/APP" stringByAppendingPathComponent: imageNameToDisk];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSLog(@"SUCCCESSFULL IMG RETRIEVE to %@!",path)

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    // Deal with failure
}];

编辑缺失

[operation start]; 

编辑 ...或者如果您使用AFHTTPClient * apiClient:

// [apiClient enqueueHTTPRequestOperation:operation];

这篇关于AFNetworking-使用AFHTTPRequestOperation下载文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆