有沒有人有使用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庫。所以如果任何人有一個想法是怎麼回事,我會非常感激。
旁註:不應MIME類型是'應用程序/八位字節流? – 2012-10-29 23:07:25
謝謝你。做出了改變,但代碼仍然無法正常工作。 – O2U