【问题标题】:Couldn't save data in given path of document directory by NSOutputStreamNSOutputStream 无法将数据保存在文档目录的给定路径中
【发布时间】:2016-03-14 08:25:13
【问题描述】:

如何通过 NSOutputStream 将下载的内容/数据保存在文档目录中的给定路径中。如果我用 DD 附加文件名,那么它可以工作(例如 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];
    }];
}

【问题讨论】:

    标签: ios objective-c nsdocumentdirectory nsoutputstream afhttprequestoperation


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2021-04-24
      • 2010-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多