你需要把你的文件放在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