Dropbox restClient声称正确保存文件,但文件在加载时不存在

时间:2014-06-06 17:09:28

标签: ios bundle dropbox-api

我是iOS新手并尝试使用Dropbox核心API:

1)从dropbox下载文件

2)从保存文件夹中收集文件并在屏幕上显示

其他客户端声称文件加载成功,但是当我尝试访问它时,它似乎不存在。这就是我正在做的事情:

在本地创建目录并加载照片:

- (void)initDBPHotos
{
    NSString *directory = @"lobyPhotos";
    [self createDirecotry:directory];
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,            
    NSUserDomainMask, YES) objectAtIndex:0];
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:directory];
    for (DBMetadata *file in self.metaData.contents) {
        [self.restClient loadFile:file.path intoPath:fullPath];
    }
    [self loadPhotoArray:fullPath];
 }

创建目录:

- (void)createDirecotry:(NSString *)dirName
{
    NSString *path;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    path = [[paths objectAtIndex:0] stringByAppendingPathComponent:dirName];
    NSError *error;
    if (![[NSFileManager defaultManager] fileExistsAtPath:path])
    {
        if (![[NSFileManager defaultManager] createDirectoryAtPath:path
                                   withIntermediateDirectories:NO
                                                    attributes:nil
                                                         error:&error])
    {
        NSLog(@"Create directory error: %@", error);
    }
  }
}

restClient文件加载成功方法:

- (void)restClient:(DBRestClient *)client loadedFile:(NSString *)localPath
       contentType:(NSString *)contentType metadata:(DBMetadata *)metadata {
    NSLog(@"File loaded into path: %@", localPath);
    int numFiles = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:localPath error:nil] count];
    NSLog(@"There are %d files in the local path", numFiles);

}

此日志的结果始终为0事件,尽管loadedFile方法始终触发说它已成功将文件加载到给定路径。

1 个答案:

答案 0 :(得分:1)

问题在于这一行:

[self.restClient loadFile:file.path intoPath:fullPath];

您的fullPath并非真正的完整路径,而是对文件夹的引用。你需要提供一个实际的完整路径。

试试这个:

for (DBMetadata *file in self.metaData.contents) {
    NSString *filename = [file.path lastPathComponent];
    NSString *destPath = [fullPath stringByAppendingPathComponent:filename];
    [self.restClient loadFile:file.path intoPath:destPath];
}

还要确保lobyPhotos文件夹中已创建Documents文件夹。