我在服务器 url 中有 mp3 文件。然后我想下载文件存储供以后访问。我正在使用 AFNetworking 从 url 下载文件。我一直在使用 AFHTTPRequestOperation 访问 url,但是我在 NSLog 中获取了 url 路径,但我没有下载文件。下载选项未执行。
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"images"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.detailsofmylife.net/wp-content/uploads/2010/02/Ellie-Goulding-Starry-Eyed-Live-Lounge-@-Radio-1.mp3"]];
NSLog(@"The video url is %@", request);
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:dataPath append:NO];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Successfully downloaded file to %@", [NSURL fileURLWithPath:dataPath]);
NSLog(@"THE RESPONSE: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error1) {
NSLog(@"Error: %@", error1);
}];
[operation start];