2012-11-22 6 views
1

AFNetworking을 사용하여 파일을 다운로드 한 후 파일을 찾는 데 문제가 있습니다 ... 다운로드 자체가 잘되지만 나중에 파일의 존재 여부를 확인할 때 찾을 수 없습니다. 그것 ... 그래서 누군가가 나를 도울 수 있기를 바랍니다.AFNetworking 다운로드 후 파일 존재

내 코드에서 파일을 다운로드하고 완료 블록에서 존재 여부를 확인합니다. 그것을 찾는 문제가, 어디 잘못 갈 수

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"url to file removed"]]; 
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 

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

//Track the progress 
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) 
{ 
    if (totalBytesExpectedToRead > 0) 
    { 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      NSString *progress = [NSString stringWithFormat:@"Downloaded %lld of %lld bytes", 
           totalBytesRead, 
           totalBytesExpectedToRead]; 

      NSLog(@"%@", progress); 
     }); 
    } 
}]; 

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) 
{ 
    NSLog(@"File downloaded to %@", path); 

    //Check for existence 
    NSFileManager *filemgr = [NSFileManager defaultManager]; 

    if([filemgr fileExistsAtPath:path]) 
    { 
     NSLog(@"File found"); 
    } else 
    { 
     NSLog(@"File not found"); 
    } 
} failure:^(AFHTTPRequestOperation *operation, NSError *error) 
{ 
    NSLog(@"Error: %@", error); 
}]; 

[operation start]; 

어떤 아이디어를 ...) 나중에 제거 할 것인가? 다운로드 자체가 잘 진행됨에 따라 문제는 iOS에 파일/파일 시스템을 저장하는 것과 관련되어 있습니다 ...

답변

5

쓰려는 디렉토리가 존재합니까? 그렇지 않은 경우 [NSOutputStream outputStreamToFileAtPath:@"filename removed" append:NO]nil을 반환 할 수 있습니다.

먼저 디렉토리를 만들어보십시오 :

NSFileManager *fm = [NSFileManager defaultManager]; 
NSString *folder = [@"filename removed" stringByDeletingLastPathComponent]; 
if (![fm fileExistsAtPath:folder]) { 
    [fm createDirectoryAtPath:folder 
    withIntermediateDirectories:YES 
        attributes:nil 
         error:nil]; 
} 

나는이 도움이되기를 바랍니다.

관련 문제