2013-01-04 114 views
6

我正在使用AFNetworking並可以成功下載文件。AFNetworking - 保存下載的文件

在下載結束時,它不會出現在我設置的目錄中。

我做了一些搜索和整個上所以這裏有幾個問題,其中有人建議我用傳來:

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

但是有錯誤出現,這是因爲據我可以告訴沒有提及他們的文件。

的錯誤是:

/用戶/傑夫/文檔/ Dropbox的-01/Dropbox的/ Xcode的項目/試前前後後 - JEFF/testDownload/testDownload/JWKDownloadViewController.m:177:10:無可見@接口「AFURLConnectionOperation」宣佈選擇「setCompletionBlockWithSuccess:失敗:」

是否有更新的行我需要使用???

+0

確保您保存文件時,其完全下載 – Dhara

+0

這就是這條線是對? _operation.outputStream = [NSOutputStream outputStreamToFileAtPath:_streamingURL append:NO]; –

+0

@Dhara這裏的文件獲取字節下載逐字節。 –

回答

4

是確保u必須使用正確的路徑爲NSOutputStream

補充一點:

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

是的,我必須檢查輸出 - 我正在保存路徑,但後來忘了添加文件名稱:-) –

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]; 
+0

這是如何在AFNetworking 3.0 ??????? – do01

+0

如何傳遞參數呢? –