【问题标题】:Reading downloaded file objective-c读取下载的文件objective-c
【发布时间】:2013-07-22 23:36:40
【问题描述】:

我正在尝试读取从我的服务器下载的文本文件。

-(void)downloadFile
{
NSURLRequest request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://myserver.com/website/file.txt"]];
AFURLConnectionOperation *operation =   [[AFHTTPRequestOperation alloc] initWithRequest:request];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"file.txt"];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:NO];

[operation setCompletionBlock:^{
        NSLog(@"downloadComplete!");

        NSString *path;
        path = [[NSBundle mainBundle] pathForResource: @"file" ofType: @"txt"];
        NSString *data = [self readFile: path];
        NSLog(@"%@",data);
}];
[operation start];
}

-(NSString *)readFile:(NSString *)fileName

{
    NSLog(@"readFile");

    NSString *appFile = fileName;
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:appFile])
    {
        NSError *error= NULL;
        NSString *resultData = [NSString stringWithContentsOfFile: appFile encoding: NSUTF8StringEncoding error: &error];
        if (error == NULL)
            return resultData;
    }
    return NULL;
}

它成功下载了文件,但我无法读取文件。返回空值。可能我无法正确设置文件路径。我想从设备磁盘读取文件,而不是项目包。

【问题讨论】:

  • 您似乎正试图从捆绑包中读取它。为什么不调用“[self readFile:filePath];”?
  • @Nicholas Hart 抱歉,我忘了添加 readFile 方法。我更新了我的帖子...
  • 您似乎正在尝试从包中读取“file.txt”,而不是从您保存它的位置。使用 NSLog 输出“error”的值(在 readFile:),你会发现我是对的。
  • 你为什么不 NSLog 实际错误是什么,而不是让它消失,用结果编辑你的问题?只是一个想法。
  • 我刚刚意识到..没有错误。它返回“null”,因为 [fileManager fileExistsAtPath:appFile] 返回 false,因为我设置的路径中不存在该文件。文件下载成功,我在模拟器的应用程序目录中看到了该文件。但是我不知道怎么称呼它..请看这个:i43.tinypic.com/wgqmao.png

标签: iphone ios objective-c download afnetworking


【解决方案1】:

改变

path = [[NSBundle mainBundle] pathForResource: @"file" ofType: @"txt"];

path = filePath;

【讨论】:

    【解决方案2】:

    我让你的东西工作了,我不知道你是否还需要它

    -(void)downloadFile
    {
        CNMRouter *routext;
    
        routext = ((LDAppDelegate *)[UIApplication sharedApplication].delegate).router;
    
        [[[Singleton sharedClient]httpClient] setParameterEncoding:AFFormURLParameterEncoding];
    
        NSMutableURLRequest *request;
    
        if([routext postOrGet]){
            request = [[[Singleton sharedClient]httpClient]  requestWithMethod:@"GET"
                                                                          path:[routext getLoginURLPath:@"admin" andPassword:@"admin"]
                                                                    parameters:nil];
        }else{
    
            request   = [[[Singleton sharedClient]httpClient]  requestWithMethod:@"POST"
                                                                            path:[routext getBackupUrl]
                                                                      parameters:[routext getBackupURLPath]];
        }
    
    
    //    NSURLRequest request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://myserver.com/website/file.txt"]];
        AFURLConnectionOperation *operation =   [[AFHTTPRequestOperation alloc] initWithRequest:request];
    
    
    
        [operation setCompletionBlock:^{
            NSLog(@"downloadComplete!");
    
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSLog(@"%@", [paths objectAtIndex:0]);
            NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"file.txt"];
    //        operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:NO];
    
            NSString *path;
    //        path = [[NSBundle mainBundle] pathForResource: @"file" ofType: @"txt"];
            path = filePath;
            NSString *data = [self readFile: path];
            NSLog(@"%@",data);
        }];
        [operation start];
    }
    

    我从它的位置移动了一些东西,但基本上你在下载文件之前就试图获取你的文件,所以我只是在 succes 块中放了一些东西。它对我有用,谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多