【问题标题】:ASIHTTP asynchrounous pdf downloadASIHTTP异步pdf下载
【发布时间】:2011-03-26 20:32:09
【问题描述】:

如果本地文件不存在,谁能提供或告诉我如何异步下载 PDF。

我的代码如下:

 NSURL *url = [NSURL URLWithString:@"http://www.url.com"]; 
NSString *tempDownloadPath = [[self documentsDirectory] 
                              stringByAppendingString:@"test.pdf"]; 
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
[request setDownloadDestinationPath:[self documentsDirectory]]; 
[request setTemporaryFileDownloadPath:tempDownloadPath]; 
[request setDelegate:self]; 
[request startAsynchronous]; 

一旦完成,我会尝试调用它

[aWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[self documentsDirectory] pathForResource:@"test" ofType:@"pdf"]isDirectory:NO]]];

但是它要么崩溃,要么没有在我的 web 视图中加载任何内容。 有什么建议吗?

使用自文档目录进行编辑

【问题讨论】:

    标签: iphone webview asihttprequest


    【解决方案1】:

    您需要将文件放在 UIWebView 可以访问的某个位置,然后将其指向那里。您没有包括如何创建 [self documentsDirectory] 并且您只是附加一个字符串而不是使用路径附加作为您的临时位置。您也没有告诉 ASIHTTPRequest 用于最终文档的实际文件名,只是将其放入的目录,因此它甚至可能没有被保存。另外,UIWebView 加载请求不正确。

    以下是如何创建路径以告诉 ASIHTTPRequest 将文件放在哪里。

    已编辑将临时文件位置更改为 NSCachesDirectory,以便在下载失败并显示部分数据时自动清除它

    // SAVED PDF PATH
    // Get the Document directory
    NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    // Add your filename to the directory to create your saved pdf location
    NSString *pdfLocation = [documentDirectory stringByAppendingPathComponent:@"test.pdf"];
    
    // TEMPORARY PDF PATH
    // Get the Caches directory
    NSString *cachesDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    // Add your filename to the directory to create your temp pdf location
    NSString *tempPdfLocation = [cachesDirectory stringByAppendingPathComponent:@"test.pdf"];
    
    // Tell ASIHTTPRequest where to save things:
    [request setTemporaryFileDownloadPath:tempPdfLocation];     
    [request setDownloadDestinationPath:pdfLocation]; 
    

    然后,当您的代理收到文件下载完成的通知时,告诉 UIWebView 在哪里可以找到文件,再次使用正确的方法。

    // If you've stored documentDirectory or pdfLocation somewhere you won't need one or both of these lines
    NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *pdfLocation = [documentDirectory stringByAppendingPathComponent:@"test.pdf"];
    
    // Now tell your UIWebView to load that file
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:pdfLocation]]];
    

    【讨论】:

      【解决方案2】:

      我认为错误在于您正在将文件下载到文档目录,然后您正在寻找主包中的文件。您应该在文档目录中查找它。

      【讨论】:

      • 你是说自己的文档目录吗?我之前试过了,还是什么都没有
      • Erik 是对的,它绝对不会出现在您的捆绑包中(捆绑项目只是应用程序捆绑并发送给 Apple 时的东西)。你是如何得到你的文件目录的?有可能你把它放在 UIWebView 不允许查看的地方。
      • @Alx,包是只读的,所以你下载的任何东西都不会在包中找到。
      • 是的,我之前尝试过,它仍然无法加载任何内容。这是使用 webview 调用存储在文件中的 pdf 的正确方法吗?查看我的编辑
      • @Alx 为了能够进一步帮助您,我认为您需要告诉我们[self documentsDirectory] 是什么。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-25
      • 2016-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-31
      • 1970-01-01
      相关资源
      最近更新 更多