读取下载的文件objective-c

时间:2013-07-22 23:36:41

标签: iphone ios objective-c download afnetworking

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

-(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;
}

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

2 个答案:

答案 0 :(得分:0)

更改

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

path = filePath;

答案 1 :(得分:0)

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

-(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];
}

我从它的位置移动了一些stuf,但基本上你试图在它甚至下载之前获取你的文件所以我只是把一些东西放在succes块中。它为我工作,谢谢!