2011-04-05 79 views
0
 -(IBAction)click; 
    { 

      NSURL *url = [NSURL URLWithString:URL_TO_DOWNLOAD]; 
      NSString *tempDownloadPath = [[self documentsDirectory] 
              stringByAppendingPathComponent:@"test.pdf"]; 
      ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
      [request setDownloadDestinationPath:[self documentsDirectory]]; 
      [request setTemporaryFileDownloadPath:tempDownloadPath]; 
      [request setDelegate:self]; 

    [request startAsynchronous]; 


     } 

    - (void)requestFinished:(ASIHTTPRequest *)request 
     { 
      NSLog(@"requestFinished"); 
      NSError *error; 
      NSFileManager *fileManager = [[NSFileManager alloc] init]; 
      NSLog(@"Documents directory: %@", [fileManager 
               contentsOfDirectoryAtPath:[self 
                      documentsDirectory] error:&error]); 
      NSString *dir = [self documentsDirectory]; 
      NSLog(dir); 
      // NSData *responseData = [request responseData]; 
      NSArray *array = [[NSFileManager defaultManager] 
           contentsOfDirectoryAtPath:dir error:&error]; 
      if (array == nil) { 
       NSLog(@"array == nil"); 
      } 
else 
{ 
[aWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:tempDownloadPath]]]; 
     [[self view] addSubview:aWebView]; 
} 

      [fileManager release]; 
       } 

    - (NSString *)documentsDirectory { 
      NSArray *paths = 
      NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
               NSUserDomainMask, 
               YES); 
      return [paths objectAtIndex:0]; 
     } 

我无法管理检查文件是否存在,然后再点击下载,或者实际显示PDF请求已完成。任何解决方案?iphone pdf下载

回答

2

要检查文件是否存在,用途:

BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:file] 

要显示PDF一旦请求完成后,您可以在UIWebView打开它,或者使用CGPDF*组函数来呈现它。

ASIHTTPRequest的setDownloadDestinationPath预计会收到一个绝对文件路径,而您似乎只是传递文档目录。 你可以从你的URL中的文件名,并追加到文件目录路径:

NSString *filename = [[url absoluteString] lastPathComponent]; 

NSString *directory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0]; 
NSString *destPath = [directory stringByAppendingPathComponent:filename]; 

[request setDownloadDestinationPath:destPath]; 

然后检查,如果下载的文件确实存在,可以再次使用destPath

0

check this out,我做了它的图像,但你可以很容易地修改它做PDF。我希望它有帮助。

+0

我试图修改文件来支持PDF,但是,它实际上不会通过简单地更改文件扩展名来加载PDF .. – 2011-04-05 14:02:00

+0

您需要使用与呈现PDF相同的方式来呈现它,查看Apple的Quartz示例 – 2011-04-07 12:06:45