简体   繁体   中英

Reading downloaded file objective-c

I am trying to read text file downloaded from my server.

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

It downloads the file successfully but I can't read file. Returns null. Probably I can't set file path correctly. I want to read file from device disk, not project bundle.

Change

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

to

path = filePath;

I made your stuff work, i dont know if you still need it

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

I moved some stuf from its place but basicaly you were trying to get your file before it was even downloaded so i just put some stuff inside the succes block. Its working for me, thank you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM