gpt4 book ai didi

ios - 无法通过 NSOutputStream 将数据保存在文档目录的给定路径中

转载 作者:行者123 更新时间:2023-11-28 21:35:12 27 4
gpt4 key购买 nike

如何通过 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];
}];
}

最佳答案

API 不会为您创建文件夹。在您尝试使用它之前,您的代码需要确保它存在。 NSFileManager 可以帮助 createDirectoryAtPath:withIntermediateDirectories:attributes:error:

关于ios - 无法通过 NSOutputStream 将数据保存在文档目录的给定路径中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34172452/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com