IOS:HTTP请求下载pdf文件

时间:2012-11-27 09:19:38

标签: ios asihttprequest home-directory

我的应用中存在问题。

我正在使用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];
}

它在FIRTS TIME工作正常,我打开第一个文件pdf没有问题,但是当我下载我在服务器上传的第二个不同的pdf时,我有一个大问题...在里面我看到两个pdf文件,一个在另一个之上,但我不明白问题是ASIHttpRequest还是readerViewController。如你所见,我删除了HomeDirectory中的文件,但我不明白这个问题,你能帮助我吗? 感谢

1 个答案:

答案 0 :(得分:0)

在你的删除方法中,你检查过路径(文件名)是否正确?此外,在检查该文件是否存在之后,再尝试删除文件的位置。

可能会提出一些想法。