我目前正在尝试使用 AFNetworking 下载一些文件,对于相对较小的文件,这似乎可行,但我正在尝试更大的文件(17MB),它似乎只是崩溃而没有任何错误。
该网址链接到一个本地文件:http://test.local/wp-content/uploads/2012/07/test.pdf(我在模拟器中运行它,所以这是可以访问的)
我得到的唯一输出是在进度块中
进度:0.009022
当我检查文件系统时,文件似乎在那里,但只有几 kb。
这是 AFNetworking 的一个已知错误,还是我只是做错了什么。
- (void)downloadIssue:(Issue *)issue
{
NSString *fileName = [issue.pdf lastPathComponent];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
NSURL *url = [NSURL URLWithString:issue.pdf];
AFHTTPClient *httpClient = [[[AFHTTPClient alloc] initWithBaseURL:url] autorelease];
NSURLRequest *request = [httpClient requestWithMethod:@"GET" path:issue.pdf parameters:nil];
AFURLConnectionOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:NO];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"PDF DOWNLOAD COMPLETE");
issue.pdf_location = filePath;
// send out a notification with the issue
[[NSNotificationCenter defaultCenter] postNotificationName:@"PDF_DOWNLOAD_COMPLETE" object:nil userInfo:[NSDictionary dictionaryWithObject:issue forKey:@"issue"]];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"PDF DOWNLOAD FAILED");
// send out a notification with the issue
[[NSNotificationCenter defaultCenter] postNotificationName:@"PDF_DOWNLOAD_FAILED" object:nil userInfo:[NSDictionary dictionaryWithObject:issue forKey:@"issue"]];
}];
[operation setDownloadProgressBlock:^(NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
float progress = (float)totalBytesRead / totalBytesExpectedToRead;
NSLog(@"progress: %f", progress);
[[NSNotificationCenter defaultCenter] postNotificationName:@"PDF_DOWNLOAD_PROGRESS" object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys: issue, @"issue", progress, @"progress", nil]];
}];
[_queue addOperation:operation];
}