2012-10-29 188 views
2

有沒有人有使用AFNetworking下載較大的文件的任何成功?這裏是我現在使用的代碼:AFNetworking和下載二進制文件

NSFileManager *fm = [NSFileManager defaultManager]; 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *dfwPath = [documentsDirectory stringByAppendingPathComponent:@"sync_download.db"]; 

//Delete the existing one if our last sync was interrupted... 
[fm removeItemAtPath:[documentsDirectory stringByAppendingPathComponent:@"sync_download.db"] error:nil]; 

NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; 
[parameters setObject:@"sync" forKey:@"app"]; 
[parameters setObject:@"dl"  forKey:@"cmd"]; 
[parameters setObject:version forKey:@"ver"]; 

[AFHTTPRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"application/bin"]]; 

AFMyClient *client = [AFMyClient sharedClient]; 

NSMutableURLRequest* rq = [client requestWithMethod:@"POST" path:@"myserver/ol.php" parameters:parameters]; 
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:rq]; 

operation.outputStream = [NSOutputStream outputStreamToFileAtPath:dfwPath append:NO]; 

[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) { 
    NSLog(@"The bytes download: %llu of %llu", totalBytesRead, totalBytesExpectedToRead); 

    //float percentDone = ((float)((int)totalBytesRead)/(float)((int)totalBytesExpectedToRead)); 

}]; 

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

    NSLog(@"Finished downloading: %@", [operation responseString]); 

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

    NSLog(@"Error downloading: %@",[error debugDescription]); 
}]; 

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

會發生什麼情況是,該文件被創建,但其即使下載量始終爲零字節約爲10MB。 「setDownloadProgressBlock」永遠不會到達,但「setCompletionBlockWithSuccess」是。

下面是我如何設置發送的下載數據之前服務器上的標題:

 $data = file_get_contents($filename); 
     header('Pragma: public'); 
     header('Content-Type: application/bin'); 
     header('Content-Disposition: attachment; filename="'.basename($filename).'";'); 
     header('Content-Transfer-Encoding: binary'); 
     header('Content-Length: '.strlen($data));  
     echo $data; 

使用這個工作完全老ASIHttpRequest庫。所以如果任何人有一個想法是怎麼回事,我會非常感激。

+0

旁註:不應MIME類型是'應用程序/八位字節流? – 2012-10-29 23:07:25

+0

謝謝你。做出了改變,但代碼仍然無法正常工作。 – O2U

回答

1

好吧我想通了。原來我不是從服務器發送我的數據最有效的方式,也是我清理了,像這樣我的HTTP頭:

 $size = filesize($filename); 
     header('Pragma: no-cache'); 
     header('Content-Type: application/octet-stream'); 
     header('Content-Disposition: attachment; filename="'.basename($filename).'";'); 
     header('Content-Transfer-Encoding: binary'); 
     header('Accept-Ranges: bytes'); 
     header('Cache-Control: max-age=604800'); 
     header("Content-Length: $size");   
     readfile($filename); 
1

三件事:

首先,你可能需要安排你的輸出流中的runloop,以確保下載的作品如預期。其次,它可能是您在該方法中創建的NSOperationQueue在操作完成之前正在解除分配。相反,您應該將您的HTTP請求操作排入AFHTTPClient操作隊列。

三,無關:您應該使用AFHTTPClient -HTTPRequestOperationWithRequest:success:failure:而不是手動創建AFHTTPRequestOperation類,並且做setCompletionBlockWithSuccess:failure:。如果不使用客戶端構造函數,則會錯過已註冊的操作邏輯和默認標頭。

+0

感謝您的提示。 – O2U