如何通过NSOutputStream将下载的内容/数据保存在文档目录中的给定路径中。如果我用DD附加文件名然后它工作(ex-DD / contentPath.zip),但如果我追加像DD / hello / contentPath.zip这样的路径,那么它就不起作用了。为什么?我尝试过如下 -
- (void) downloadCarContents:(NSString *)url forContent:(NSString *)contentPath {
//NSString *destinationPath = [self.documentDirectory getDownloadContentPath:contentPath];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFHTTPRequestOperation *op = [manager GET:url
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//NSLog(@"Error : %@", error.localizedDescription);
}];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *dest = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"hello/%@.zip", contentPath]];// It doesn't work
//NSString *dest = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip", contentPath]];// It works
op.outputStream = [NSOutputStream outputStreamToFileAtPath:dest append:NO];
[op setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
float percentage = (float) (totalBytesRead * 100) / totalBytesExpectedToRead;
[self.delegate downloadProgression:percentage];
}];
}
答案 0 :(得分:2)
API不会为您创建文件夹。在尝试使用它之前,您的代码需要确保它存在。 NSFileManager
可以帮助createDirectoryAtPath:withIntermediateDirectories:attributes:error: