【问题标题】:How to copy a file from URL to document folder?如何将文件从 URL 复制到文件夹?
【发布时间】:2014-03-04 11:53:12
【问题描述】:

我需要从 URL 复制文本文件并将其放置/覆盖在我的应用程序的文档文件夹中,然后将其读回数据变量。 我有以下代码:

NSData *data;

//get docsDir
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir=[paths objectAtIndex:0];

//get path to text.txt
NSString *filePath=[docsDir stringByAppendingPathComponent:@"text.txt"];

//copy file
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;

if([fileManager fileExistsAtPath:filePath]==YES){
    [fileManager removeItemAtPath:filePath error:&error];
}

NSString *urlText = @"http://www.abc.com/text.txt";

if (![[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    [fileManager copyItemAtPath:urlText toPath:filePath error:NULL];
}

//Load from file
NSString *myString=[[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];

//convert string to data
data=[myString dataUsingEncoding:NSUTF8StringEncoding];

它以良好的方式构建和遵守,但我无法在我的文档文件夹中创建 text.txt 文件,然后将任何内容传递给我的数据变量。 我是IOS和Xcode的新手,任何线索将不胜感激。谢谢!!

【问题讨论】:

    标签: iphone objective-c nsfilemanager


    【解决方案1】:

    您应该从 URL 中获取数据并使用 WriteToFile

     NSData *urlData = [NSData dataWithContentsOfURL: [NSURL URLWithString:urlText]];
        [urlData writeToFile:filePath atomically:YES];
    

    【讨论】:

    • 感谢您的意见。我已经尝试过,但是“urlText”与此不兼容,因为它需要一个 NSURL 类型才能与 dataWithContentsOfURL 一起使用...我更改如下但仍然不起作用: NSData *urlData = [NSData dataWithContentsOfFile: urlText]; [urlData writeToFile:filePath atomically:YES];
    • 您应该将 urlText(字符串)转换为 url。请查看答案
    【解决方案2】:

    NSFileManager 只能处理本地路径。如果你给它一个 URL,它不会做任何有用的事情。

    copyItemAtPath:toPath:error: 采用错误参数。像这样使用它:

    NSError *error;
    if (![fileManager copyItemAtPath:urlText toPath:filePath error:&error]) {
        NSLog(@"Error %@", error);
    }
    

    然后你会得到这个错误:

    Error Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be
    completed. (Cocoa error 260.)" UserInfo=0x9a83c00 {NSFilePath=http://www.abc.com/text.txt, 
    NSUnderlyingError=0x9a83b80 "The operation couldn’t be completed. 
    No such file or directory"}
    

    它无法读取位于http://www.abc.com/text.txt 的文件,因为它不是有效路径。


    正如 Sunny Shah 所说,您必须先在 URL 处获取对象:

    NSString *urlText = @"http://www.abc.com/text.txt";
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:filePath])
    {
        NSURL *url = [NSURL URLWithString:urlText];
        NSError *error;
        NSData *data = [[NSData alloc] initWithContentsOfURL:url options:0 error:&error];
        if (!data) { // check if download has failed
            NSLog(@"Error fetching file %@", error);
        }
        else {
            // successful download
            if (![data writeToFile:filePath options:NSDataWritingAtomic error:&error]) { // check if writing failed
                NSLog(@"Error writing file %@", error);
            }
            else {
                NSLog(@"File saved.");
            }
        }
    }
    

    始终检查错误!

    【讨论】:

    • 效果很好,感谢 Matthias 和 Sunny!这几天我已经工作了!此外,我的 Xcode 调试控制台没有显示任何内容。我不知道为什么..
    猜你喜欢
    • 1970-01-01
    • 2016-02-28
    • 2011-08-15
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    • 2017-03-31
    • 2011-06-23
    • 2019-03-02
    相关资源
    最近更新 更多