2011-11-04 6 views
16

人、この人に困った。 AFNetworkingでファイルをダウンロードしようとしていますが、できることは、downloadProgressBlockの進行バイトをキャプチャすることです。私はAFNetworkingのさまざまなフォークを試しましたが、今は最新のビルドに戻っています。 AFHTTPRequestOperationがNSURLResponseもインクルードするように修正されたようですが、最新のリリースではなくなりました。また、以下のコードでは、「成功」ブロックが呼び出されることはありません。私はダウンロードされたバイトのログを取得し、次に^ completeが呼び出されます。成功とエラーは決して呼び出されません。AFNetworking - AFHTTPRequestOperationを使用してファイルをダウンロードしますか?

これについてのガイダンスはすばらしいでしょう。私はデータがどこに返されるのか、NSFileManagerを使用する方法を理解できません。私はファイルをダウンロードする必要があります。画像のストリームは書きません。

編集:私はまた、ドキュメントに示唆されているように - (void)connection:didReceiveDataをオーバーライドしてデータをキャプチャしようとしました。

// URLは、単にAFNetworkingに含まれているこの機能を使用しても、それをディスクに保存することができます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]; 

答えて

64

ある - お知らせ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 
}]; 

はEDITを: missed

[operation start]; 

EDIT...or if you use an AFHTTPClient * apiClient :

// [apiClient enqueueHTTPRequestOperation:operation]; 
+3

誰かがこの男にupvoteを与えます!そしてOPは答えを選ぶべきです! – radj

関連する問題