-(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。任何解决方案吗?
答案 0 :(得分: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
。
答案 1 :(得分:0)
check this out,我是为图像制作的,但您可以轻松地将其修改为PDF。我希望它有所帮助。