iphone pdf下载
我无法检查文件是否存在,然后单击再次下载,或实际显示请求已完成的pdf文件。对此有何解决方案?要检查文件是否存在,请使用:iphone pdf下载,iphone,pdf,asihttprequest,Iphone,Pdf,Asihttprequest,我无法检查文件是否存在,然后单击再次下载,或实际显示请求已完成的pdf文件。对此有何解决方案?要检查文件是否存在,请使用: -(IBAction)click; { NSURL *url = [NSURL URLWithString:URL_TO_DOWNLOAD]; NSString *tempDownloadPath = [[self documentsDirectory]
-(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,可以在UIWebView
中打开它,或者使用CGPDF*
函数集渲染它
ASIHTTPRequest的setDownloadDestinationPath
希望收到一个绝对文件路径,而您似乎只是传递documents目录。
您可以从URL获取文件名并将其附加到文档目录路径:
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:file]
然后,要检查下载的文件是否确实存在,可以再次使用destPath
NSString *filename = [[url absoluteString] lastPathComponent];
NSString *directory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0];
NSString *destPath = [directory stringByAppendingPathComponent:filename];
[request setDownloadDestinationPath:destPath];