2013-04-20 77 views
0

我试图从我的服务器使用AFNetworking下载自定义文件类型 - 出于某种原因,它说它成功下载了文件,但它看起来像结果已损坏。AFNetworking下载自定义文件

这是我在日志中得到:

2013-04-20 11:17:40.326 app[18083:907] start downloads 
2013-04-20 11:17:40.392 app[18083:907] Sent 283280 of 283280 bytes, /var/mobile/Applications/187878BC-9D5A-4E1C-9EE4-34512D93BF68/Documents/theprice.cm 
2013-04-20 11:17:40.393 app[18083:907] Successfully downloaded file to /var/mobile/Applications/187878BC-9D5A-4E1C-9EE4-34512D93BF68/Documents/theprice.cm 

我的网络连接速度快,但它肯定没有在0.04秒下载283280个字节。

这里是我的AFNetworking代码:

NSLog(@"start downloads"); 

NSString *urlpath = [NSString stringWithFormat:@"http://www.domain.com/ygp6bcckll8mw62.cm"]; 
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlpath]]; 

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"theprice.cm"]]; 
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 setDownloadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { 
    NSLog(@"Sent %lld of %lld bytes, %@", totalBytesWritten, totalBytesExpectedToWrite, path); 
}]; 

[operation start]; 

当我尝试访问该文件的内容,正如人们所预料:它是空的。

有关为什么这不正确下载自定义文件类型的任何想法? (.cm)我是否遗漏了AFNetworking代码中的某些内容以使其行为异常?

回答

0

你确定outputStream正在被适当的冲洗和关闭?一旦收到所有数据后,AFHTTPRequestOperation可能会将传输标记为已成功完成,但在写入永久性存储时没有任何特别的延迟和/或要求意识。

0

AFNetworking不会向其输出流发送开放消息。尽管如此,它发送完毕后或发生错误时会向其发送close消息。在开始请求之前,您可以尝试将open发送到输出流,这将有所帮助。

否则会发出警告,AFNetworking不会检查流中潜在的失败代码,也不能保证所有接收到的字节都已经完全写入流中。