【问题标题】:IOS: delete a file in a directoryIOS:删除目录中的文件
【发布时间】:2012-11-11 17:55:45
【问题描述】:

在我的应用程序中,我下载了一个带有 ASiHttpRequest 的 pdf 文件,并且我有以下说明:

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    [currentDownload setDownloadDestinationPath:[documentsDirectory stringByAppendingPathComponent:@"file.pdf"]];

第一次运行正常,我可以打开这个file.pdf,但是当我第二次下载这个pdf时,它似乎没有替换文件而是进行了合并。

在我这样做之前,但它不起作用问题出在哪里,或者从其路径中删除此 file.pdf 的最佳方法是什么?

    - (void) removeFile{
  NSString *extension = @"pdf";
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectoryPDF = [paths objectAtIndex:0];

    NSArray *contents = [fileManager contentsOfDirectoryAtPath:documentsDirectoryPDF error:NULL];
    NSEnumerator *e = [contents objectEnumerator];
    NSString *filename;
    while ((filename = [e nextObject])) {

        if ([[filename pathExtension] isEqualToString:extension]) {

            [fileManager removeItemAtPath:[documentsDirectoryPDF stringByAppendingPathComponent:filename] error:NULL];
        }
    }
}

编辑

现在我用这个方法

- (void) removeFile{


    NSError *error;

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
    NSString *path = [[paths objectAtIndex:0]stringByAppendingString:@"/file.pdf"];

    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSLog(@"Documents directory before: %@", [fileManager contentsOfDirectoryAtPath:[paths objectAtIndex:0] error:&error]);


        if([fileManager fileExistsAtPath:path] == YES)
        {
            NSLog(@"file exist and I delete it");

            NSFileManager *fileManager = [NSFileManager defaultManager];
            [fileManager removeItemAtPath:path error:&error];

            NSLog(@"error:%@", error);
        }

    NSLog(@"Documents directory after: %@", [fileManager contentsOfDirectoryAtPath:[paths objectAtIndex:0] error:&error]);


}

此方法识别目录中的NSLog中有“file.pdf”

NSLog(@"Documents directory before: %@", [fileManager contentsOfDirectoryAtPath:[paths objectAtIndex:0] error:&error]);

但是在

之后它崩溃了
"NSLog(@"file exist and I delete it");" 

我在控制台中只有一个“lldb”。

【问题讨论】:

  • 可能问题在于ASIHttpRequest 检测到文件存在并假设您正在恢复下载,尝试将allowResumeForFileDownloads 设置为NO
  • 嗯嗯,不行……
  • ok,另一个选项可能是缓存问题,你可以检查属性didUseCachedResponse的值来检查响应是否从缓存中获得,并与cachePolicy和@一起玩987654331@你可以启用/禁用缓存

标签: ios nsdocumentdirectory


【解决方案1】:

我使用此方法从本地缓存中删除 pdf 文件,稍作修改即可使其适应您的需要

- (void)removePDFFiles
{
    NSFileManager *fileMngr = [NSFileManager defaultManager];

    NSArray *cacheFiles = [fileMngr contentsOfDirectoryAtPath:[self cacheDirectory]
                                                    error:nil];
    for (NSString *filename in cacheFiles) {
        if ([[[filename pathExtension] lowercaseString] isEqualToString:@"pdf"]) {
            [fileMngr removeItemAtPath:[NSString stringWithFormat:@"%@/%@", [self cacheDirectory], filename] error:nil];
        }
    }
}

【讨论】:

    【解决方案2】:

    很可能您在下载新文件之前没有删除该文件,ASIHttpRequest 会看到已经有一个同名文件并将数据附加到该文件而不是替换该文件。我不确定 PDF 格式,但这通常不会导致合并的 可读 文件。无论如何,首先您需要使用文件管理器类为您提供的错误机制。传递 NULL 是不好的。很坏。因此,创建一个 NSError 对象并将其传递给contentsOfDirectoryAtPathremoveItemAtPath 方法,然后检查错误,确保操作成功完成。之后,您可能还需要检查扩展名是否大写,因为基于 Unix 的系统区分大小写(尽管模拟器不区分,但设备区分)并且不会根据您的代码删除 example.PDF 文件。

    【讨论】:

    • 是的,这正是我遇到的问题...我尝试了错误但没有错误...所以我尝试使用大写的文件扩展名...
    • 我的“contentsOfDirectoryAtPath”有一个错误,它是错误日志:error:Error Domain=NSCocoaErrorDomain Code=256“操作无法完成。(Cocoa 错误 256。)”UserInfo= 0xbc40500 {NSUnderlyingError=0xbc40e70“操作无法完成。不是目录”,NSFilePath=/var/mobile/Applications/345B9530-74FF-49DE-AF39-418D03C9A149/Documents/file.pdf,NSUserStringVariant=(文件夹跨度>
    • documentsDirectoryPDF 不应该是 /var/mobile/Applications/345B9530-74FF-49DE-AF39-418D03C9A149/Documen‌​ts/file.pdf 你能确定它的价值吗?
    【解决方案3】:

    尝试以下方法:

    -(BOOL) removePDF
    {
        BOOL removeStatus = NO;
    
        NSArray *dirPaths;
        NSString *docsDir;
    
        dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                                                   NSUserDomainMask, YES);
    
        docsDir = [dirPaths objectAtIndex:0];
    
        NSString* fileName = @"file.pdf"; //your file here..
    
        NSString* filePath = [NSString stringWithFormat:@"%@/%@", docsDir, fileName];
    
        if([[NSFileManager defaultManager] fileExistsAtPath:filePath] == YES)
        {
           removeStatus = [[NSFileManager defaultManager] removeItemAtPath:filePath]; 
        }
        return removeStatus;
    }
    

    【讨论】:

    • @blackguardian:你解决了这个问题吗?问题的原因是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-26
    • 2013-05-30
    • 2017-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多