ASIHTTP异步pdf下载
如果本地文件不存在,任何人都可以提供或告诉我如何异步下载 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]]];
但它要么崩溃,要么不在我的网络视图中加载任何内容。 有什么建议吗?
使用自我文档目录进行编辑
Can anyone please provide or show me how to download a PDF asynchronously if a local file doesnt exist.
My code is as follows:
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?
EDIT WITH SELF DOCUMENTSDIRECTORY
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将文件放在 UIWebView 可访问的某个位置,然后将其指向那里。您没有包含创建
[self DocumentsDirectory]
的方式,并且您只是附加一个字符串,而不是使用临时位置的路径附加。您也没有告诉 ASIHTTPRequest 最终文档使用什么实际文件名,只是将其放入的目录,因此它甚至可能不会被保存。此外,UIWebView加载请求不正确。以下是如何创建路径来告诉 ASIHTTPRequest 将文件放在哪里。
编辑将临时文件位置更改为 NSCachesDirectory,以便在下载失败且部分数据失败时自动清除
然后,当您的代理收到文件下载完成的通知时,告诉 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.Here's how to create your path for telling ASIHTTPRequest where to put the file.
EDITED to change temporary file location to the NSCachesDirectory instead, so that it will be automatically cleared out if the download fails with partial data
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.
我认为错误在于您将文件下载到文档目录,然后在主包中查找该文件。您应该在文档目录中查找它。
I think the error is that you're downloading the file to the documents directory and then you're looking for the file in the main bundle. You should look for it in the documents directory.