0

我的应用程序有问题。

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

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

4

1 回答 1

0

在您的删除方法中,您是否检查过路径(带有文件名)是否正确?另外,在删除文件的地方再尝试一件事,然后检查该文件是否存在。

可能会给一些想法。

于 2012-11-27T11:35:54.003 回答