我想从某个特定的URL下载NSFileManager中的文件(pdf,doc,docx等)。稍后,当用户点击同一链接时,我想从NSFileManager中检索文件。以下是我已完成的代码:
- (void)checkIfFileExist {
NSString *path1;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
path1 = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Content-Directory"];
path1 = [path1 stringByAppendingPathComponent:@"sample.pdf"];
NSLog(@"The read path is %@", path1);
if ([[NSFileManager defaultManager] fileExistsAtPath:path1])
{
//File exists
NSData *file1 = [[NSData alloc] initWithContentsOfFile:path1];
if (file1)
{
}
}
else
{
NSURL *url = [NSURL URLWithString:@"http://fzs.sve-mo.ba/sites/default/files/dokumenti-vijesti/sample.pdf"];
NSData *urlData = [NSData dataWithContentsOfURL:url];
NSString *path;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Content-Directory"];
path = [path stringByAppendingPathComponent:@"sample.pdf"];
NSLog(@"The written path is %@", path);
[[NSFileManager defaultManager] createFileAtPath:path
contents:urlData
attributes:nil];
}
}
现在,每当我尝试从文件管理器获取文件时,文件存在的条件总是返回FALSE。我写和读文件的路径与我在控制台上检查过的路径相同。我做过的任何错误?
答案 0 :(得分:0)
添加此
foo
之后
if (![[NSFileManager defaultManager] fileExistsAtPath:path]){[[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:nil];}
在你的其他部分并替换
path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Content-Directory"];