获取URL的所有文件夹

时间:2011-04-06 14:09:16

标签: iphone objective-c nsfilemanager

您好我正在尝试从给定服务器读取所有文件。我想做什么:

  • 阅读所有文件夹
  • 获取文件夹
  • 中的文件网址

我试过这个来获取我的服务器的文件夹和文件,但是它返回了一个包含我的MacBook文件夹的数组:

NSURL *directory = [NSURL URLWithString:@"linktoserver"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = nil;
self.contentList = [[NSArray alloc] initWithArray:[fileManager contentsOfDirectoryAtURL:directory includingPropertiesForKeys:[[NSArray alloc] initWithObjects:NSURLNameKey, nil] options:NSDirectoryEnumerationSkipsHiddenFiles error:&error]];
if (error != nil) {
    NSLog(@"ERROR: %@",[error localizedDescription]);
}
NSLog(@"%@",contentList);

日志:

> 2011-04-06 15:37:38.413 Bildergalerie[744:207] (
>     "file://localhost/Applications/",
>     "file://localhost/Benutzerhandbu%CC%88cher%20und%20Informationen",
>     "file://localhost/Cancel",
>     "file://localhost/Developer/",
>     "file://localhost/Library/",
>     "file://localhost/opt/",
>     "file://localhost/Shockwave%20Log",
>     "file://localhost/System/",
>     "file://localhost/Users/",
>     "file://localhost/usr/" )

任何人都可以帮我找到答案吗?我真的很困惑,谷歌没有找到一个好的解决方案或教程。

非常感谢, mavrick3。

3 个答案:

答案 0 :(得分:1)

您无法使用NSFileManager执行此操作。 NSFileManager旨在与您的文件系统(您的设备的文件系统)一起使用,而不是与服务器一起使用。

您需要创建一个服务器端文件,该文件应该为您提供xml文件中的文件夹/文件URL

答案 1 :(得分:0)

因此,如果我理解正确,您需要阅读该文件夹中所有文件夹的列表,但现在您需要每个文件夹中的特定文件?

那你为什么不创建一个for-loop?

for (int i = 0; i < [contentlist count]; i++
{
    //do your trick to get the file in the folder
    //save it
}

编辑:如果您的意思是从Mac获取文件而不是您需要的服务器,那么您应该将行contentsOfDirectoryAtURL:directory更改为contentsOfDirectoryAtURL:@"http://localhost/myserver/

答案 2 :(得分:0)

您无法使用NSFileManager访问远程文件夹。该类只能访问本地目录。
如果您的服务器支持FTP连接,那么您可以使用CFFTPStream CFFTPStream Reference

相关问题