Ios 不能';t通过NSOutputStream将数据保存在文档目录的给定路径中
如何通过NSOutputStream将下载的内容/数据保存在文档目录的给定路径中。如果我用DD附加文件名,那么它可以工作(ex-DD/contentPath.zip),但是如果我附加像DD/hello/contentPath.zip这样的路径,那么它就不能工作。为什么?我试着像下面一样-Ios 不能';t通过NSOutputStream将数据保存在文档目录的给定路径中,ios,objective-c,nsdocumentdirectory,nsoutputstream,afhttprequestoperation,Ios,Objective C,Nsdocumentdirectory,Nsoutputstream,Afhttprequestoperation,如何通过NSOutputStream将下载的内容/数据保存在文档目录的给定路径中。如果我用DD附加文件名,那么它可以工作(ex-DD/contentPath.zip),但是如果我附加像DD/hello/contentPath.zip这样的路径,那么它就不能工作。为什么?我试着像下面一样- - (void) downloadCarContents:(NSString *)url forContent:(NSString *)contentPath { //NSString *destin
- (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];
}];
}
API不会为您创建文件夹。您的代码在尝试使用它之前需要确保它存在
NSFileManager
可以帮助