2012-11-27 144 views
0

我在我的應用程序中遇到問題。IOS:HTTPRequest下載pdf文件

我使用ASIHttpRequest下載這段代碼的PDF文件

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

經典控制器打開iPhone的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]; 
} 

它運行良好的FIRST時間,我打開第一個文件pdf沒有問題,但是當我下載第二個不同的pdf,我上傳到服務器上,我有一個大問題...裏面我看到兩個PDF文件,一個在另一個之上,但我不明白問題是ASIHttpRequest還是readerViewController。正如你所看到的,我刪除了HomeDirectory中的文件,但是我不明白這個問題,你能幫助我嗎? 謝謝

回答

0

在你的remove方法中,你檢查了路徑(帶有文件名)是否正確?另外,請嘗試刪除文件的地方,然後檢查該文件是否存在。

可能會提出一些想法。

+0

yes它刪除正確的文件,我也檢查了在這個路徑的openFile方法打開的文件的大小「NSString * path = [pathTemp stringByAppendingPathComponent:@」file.pdf「];」,但它永遠是相同的大小,這是正確的 – CrazyDev

+0

在你的刪除文件你的pathTemp日誌,請告訴。 –

+0

NSString *文件; 而(文件= [烯nextObject]){ 解析度= [FM removeItemAtPath:[pathTemp stringByAppendingPathComponent:文件]錯誤:&err]; 如果(RES && ERR!){ 的NSLog(@ 「錯誤:%@」,ERR); } }在這裏檢查你沒有提供你想要刪除的文件名。 –