你需要把你的文件放在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]]];
@Alx是否为您工作? – 2011-03-31 02:48:45