2012-11-27 137 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); } }在这里检查你没有提供你想要删除的文件名。 –