ASIHTTP asynchrounous pdf下载 [英] ASIHTTP asynchrounous pdf download

查看:161
本文介绍了ASIHTTP asynchrounous pdf下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果本地文件不存在,任何人都可以提供或告诉我如何异步下载PDF。

Can anyone please provide or show me how to download a PDF asynchronously if a local file doesnt exist.

我的代码如下:

 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]; 

一旦完成,我试着打电话给这个

Once it is complete I try and call this

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

但是它会在我的网页视图中崩溃或加载任何内容。
有什么建议吗?

however it either crashes or doesn't load anything inside my web view. Any suggestions?

编辑自己的文件目录

推荐答案

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

You need to put your file in some place accessible to the UIWebView and then point it there. You've not included how you're creating [self documentsDirectory] and you're just appending a string rather than using the path append for your temporary location. You're also not telling ASIHTTPRequest what actual file name to use for the final document, just the directory to put it in, so it's likely not even being saved. Additionally, the UIWebView load request is incorrect.

以下是如何创建路径以告知ASIHTTPRequest放置文件的位置。

Here's how to create your path for telling ASIHTTPRequest where to put the file.

已编辑以将临时文件位置更改为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在哪里找到文件,再次使用正确的方法。

Then when your delegate receives notification of the file download being complete, tell the UIWebView where to find the file, again using the proper methods.

// 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]]];

这篇关于ASIHTTP asynchrounous pdf下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆