2012-11-27 2 views
0

내 응용 프로그램에 문제가 있습니다.IOS : PDF 파일을 다운로드하는 HTTPRequest

이 코드와 pdf 파일 다운로드 ASIHttpRequest을 사용하고 있습니다 :

- (void) downloadPdf{ 
AppDelegate *appDelegate = (AppDelegate*) [UIApplication sharedApplication].delegate; 
if (currentDownload) { 

    [currentDownload clearDelegatesAndCancel]; 
    currentDownload = nil; 
    totalWorker = 0; 
    [workers release]; workers = nil; 
    progressView.progress = 0.0; 

} else { 

    [self removeFile]; 

    appDelegate.alertGlobal = [[UIAlertView alloc] initWithTitle: @"Download file..." 
               message: @"" 
               delegate: self 
             cancelButtonTitle: @"Annul" 
             otherButtonTitles: nil]; 

    progressView = [[UIProgressView alloc] initWithFrame: 
        CGRectMake(30.0f, 43.0f, 225.0f, 10.0f)]; 
    [appDelegate.alertGlobal addSubview:progressView]; 
    [progressView setProgressViewStyle: UIProgressViewStyleBar]; 

    [appDelegate.alertGlobal show]; 

    NSString *tmp = @"http://192.168.0.13:8888/file.pdf"; 

    currentDownload = [[HappyDownload alloc] initWithURL:[NSURL URLWithString:tmp]]; 
    currentDownload.numberOfWorkers = totalWorker; 
    currentDownload.totalChunk = 1; 
    currentDownload.delegate = self; 
    currentDownload.downloadProgressDelegate = self; 
    currentDownload.allowResumeForFileDownloads = YES; 

    documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; 

    [currentDownload setDownloadDestinationPath:[documentsDirectory stringByAppendingPathComponent:@"file.pdf"]]; 

    [workers removeAllObjects]; 

    [currentDownload startAsynchronous]; 

    start = [NSDate new]; 
} 
} 



- (void) removeFile{ 

    NSString *pathTemp = [NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]; 
    //NSString *path = [pathTemp stringByAppendingPathComponent:@"file.pdf"]; 

    NSFileManager* fm = [[[NSFileManager alloc] init] autorelease]; 
    NSDirectoryEnumerator* en = [fm enumeratorAtPath:pathTemp]; 
    NSError* err = nil; 
    BOOL res; 

    NSString* file; 
    while (file = [en nextObject]) { 
     res = [fm removeItemAtPath:[pathTemp stringByAppendingPathComponent:file] error:&err]; 
     if (!res && err) { 
      NSLog(@"error: %@", err); 
     } 
    } 

} 



-(void)requestFinished:(ASIHTTPRequest *)request{ 
    NSDateComponents *comps = [[NSCalendar currentCalendar] components:NSSecondCalendarUnit 
                   fromDate:start 
                   toDate:[[NSDate new] autorelease] 
                   options:0]; 
    int duration = [comps second]; 

    NSLog(@"request finished, duration:%d", duration); 

    AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate; 
    appDelegate.downloaded = TRUE; 

    [currentDownload clearDelegatesAndCancel]; 
    currentDownload = nil; 
    totalWorker = 0; 
    [workers release]; workers = nil; 
    progressView.progress = 0.0; 

    [appDelegate.alertGlobal dismissWithClickedButtonIndex:0 animated:YES]; 
    [appDelegate.alertGlobal release]; 

    downloadedFirstTime = TRUE; 

    NSError *error; 
    NSString *pathTemp = [NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSLog(@"Documents directory: %@", [fileManager contentsOfDirectoryAtPath:pathTemp error:&error]); 

    [self openFile]; 

} 

- (void) requestFailed:(ASIHTTPRequest *)request{ 
    AppDelegate *appDelegate = (AppDelegate*) [UIApplication sharedApplication].delegate; 
    [currentDownload clearDelegatesAndCancel]; 
    currentDownload = nil; 
    totalWorker = 0; 
    [workers release]; workers = nil; 
    progressView.progress = 0.0; 

    [appDelegate.alertGlobal dismissWithClickedButtonIndex:0 animated:YES]; 
    [appDelegate.alertGlobal release]; 

    NSLog(@"request failed"); 
    NSLog(@"Error %@", [request error]); 
    int statusCode = [request responseStatusCode]; 
    NSString *statusMessage = [request responseStatusMessage]; 
    NSLog(@"statuscode:%d", statusCode); 
    NSLog(@"statusmessage:%@", statusMessage); 
} 

이 방법으로 확인 내가 제대로 PDF 파일을 다운로드하고 난이 컨트롤과이 방법을 엽니 다

https://github.com/vfr/Reader

클래식 컨트롤러는 아이폰에서 PDF 파일을 엽니 다 나는이 방법을 사용

- (void) openFile{ 

    AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate; 

    NSString *phrase = nil; // Document password (for unlocking most encrypted PDF files) 

    NSString *pathTemp = [NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]; 
    NSString *path = [pathTemp stringByAppendingPathComponent:@"file.pdf"]; 

    assert(path != nil); // Path to last PDF file 

    ReaderDocument *document = [ReaderDocument withDocumentFilePath:path password:phrase]; 

    if (document != nil) // Must have a valid ReaderDocument object in order to proceed 
    { 
     readerViewController = [[ReaderViewController alloc] initWithReaderDocument:document]; 

     readerViewController.delegate = self; // Set the ReaderViewController delegate to self 

     //mainWindow.rootViewController = readerViewController; // Set the root view controller 
    } 

    [self presentModalViewController:readerViewController animated:YES]; 

    [readerViewController release]; 
} 
을 0

FIRTS TIME이 잘 작동하고 문제없이 첫 번째 파일을 열면 두 번째 다른 pdf 파일을 다운로드 할 때 큰 문제가 있습니다 ... 두 개의 pdf 파일이 있는데, 하나 이상의 다른,하지만 문제가 ASIHttpRequest 또는 readerViewController 경우 이해가되지 않습니다. 당신이 볼 수 있듯이 HomeDirectory 내부의 파일을 제거하지만 문제를 이해하지 못한다면 도움을 줄 수 있습니까? 감사합니다.

답변

0

제거 방법에서 경로 (파일 이름 포함)가 올바른지 확인 했습니까? 또한, 그 파일의 존재를 확인한 후에 파일을 삭제하는 장소를 하나 더 시도하십시오.

몇 가지 아이디어가있을 수 있습니다.

+0

예 파일을 올바르게 제거하고이 경로에서 openFile 메서드로 열어 본 파일의 크기도 확인했습니다. "NSString * path = [pathTemp stringByAppendingPathComponent : @"file.pdf "];" 같은 크기, 맞습니다 – CrazyDev

+0

파일에서 pathTemp 로그를 확인하십시오. –

+0

NSString * 파일; 동안 (파일 = EN nextObject]) { 입술 = [FM의 removeItemAtPath [pathTemp stringByAppendingPathComponent : 파일] 에러 : &err]; 경우 NSLog (@ "오류 % @", ERR) (입술 && ERR!) {; } } 지우려는 파일 이름을 여기에서 확인하십시오. –

관련 문제