0

サーバーからダウンロードしたテキスト ファイルを読み込もうとしています。

-(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 を返します。おそらく、ファイルパスを正しく設定できません。プロジェクト バンドルではなく、デバイス ディスクからファイルを読み取りたい。

4

2 に答える 2

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

私はその場所からいくつかのものを移動しましたが、基本的にあなたはファイルがダウンロードされる前にファイルを取得しようとしていたので、succes ブロック内にいくつかのものを入れました。私のために働いています、ありがとう!

于 2013-12-12T20:01:41.970 に答える