获取URL的所有文件夹
您好,我正在尝试从给定服务器读取所有文件。我想要做什么:
- 读取所有文件夹
- 获取文件夹内的文件 URL
我尝试使用此方法获取服务器的文件夹和文件,但它返回了一个包含 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/" )
任何人都可以帮助我找到答案了吗?我真的很困惑,谷歌没有找到好的解决方案或教程。
多谢, 马夫里克3。
Hi I'm trying to read all files from a given server. What I want to do:
- Read all the folders
- Get the file URLs inside the folders
I tried this to get the folders and filey of my server, but it returned me an array with the folders of my 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);
Log:
> 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/" )
Can anyone help me to find the answer? I'm really confused and Google didn't find a good solution or tutorial.
Thanks a lot,
mavrick3.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你不能用 NSFileManager 来做到这一点。 NSFileManager 旨在与您的文件系统(您设备的文件系统)一起使用,而不是与服务器一起使用。
您需要创建一个服务器端文件,该文件应在 xml 文件中为您提供文件夹/文件 url
You can't do this with NSFileManager. NSFileManager is intended to work with your file system (your device's file system) not with server.
you need to create a server side file which should give you folder/files url in xml file
您无法使用 NSFileManager 访问远程文件夹。该类只能访问本地目录。
如果您的服务器支持 FTP 连接,那么您可以使用 CFFTPStream。
CFFTPStream 参考
You can not make use of NSFileManager to access the remote folders. This class is able to access the local directories only.
If your server supports FTP connection then you can make use of CFFTPStream.
CFFTPStream Reference
所以,如果我理解正确的话,您拥有需要读取的文件夹中所有文件夹的列表,但现在您需要每个文件夹中的特定文件?
那为什么不创建一个for循环呢?
编辑:如果您的意思是从 Mac 而不是所需的服务器获取文件,那么您应该将
contentsOfDirectoryAtURL:directory
行更改为contentsOfDirectoryAtURL:@"http://localhost /myserver/
。So if I understand correctly, you have the list of all the folders in the folder you need to read, but now you need specific files from each folder?
Then why don't you create a for-loop?
EDIT: If you mean you get the files from your Mac instead of the server you need, then you should change the line
contentsOfDirectoryAtURL:directory
to something likecontentsOfDirectoryAtURL:@"http://localhost/myserver/
.