我正在嘗試開發一個下載管理器。我現在可以從幾乎任何地方通過linkclick下載文件。從某些文件主機接收數據的NSUrlconnection問題
在 - (BOOL)web視圖:(的UIWebView *)webView的shouldStartLoadWithRequest:(的NSURLRequest *)請求navigationType:(UIWebViewNavigationType)navigationType 我檢查如果URL是一個URL到像zip文件一個binaryfile。比我設置一個NSURLConnection的
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:20.0];
[urlRequest setValue:@"User-Agent" forHTTPHeaderField:@"Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en) AppleWebKit/418.9 (KHTML, like Gecko) Safari/419.3"];
NSURLConnection *mainConnection = [NSURLConnection connectionWithRequest:urlRequest delegate:self];
if (nil == mainConnection) {
NSLog(@"Could not create the NSURLConnection object");
}
(void)connection:(NSURLConnection )connection didReceiveResponse:(NSURLResponse)response { self.tabBarController.selectedIndex=1; [receivedData setLength:0]; percent = 0; localFilename = [[[url2 absoluteString] lastPathComponent] copy]; NSLog(localFilename);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0] ;
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:localFilename];
[[NSFileManager defaultManager] createFileAtPath:appFile contents:nil attributes:nil]; [downloadname setHidden:NO]; [downloadname setText:localFilename]; expectedBytes = [response expectedContentLength]; exp = [response expectedContentLength]; NSLog(@"content-length: %lli Bytes", expectedBytes); file = [[NSFileHandle fileHandleForUpdatingAtPath:appFile] retain]; if (file) {
[file seekToEndOfFile];
} }
(無效)連接:(NSURLConnection的*)連接didReceiveData:(NSData的*)數據{ 如果(文件){ [文件seekToEndOfFile] ; } [file writeData:data]; [receivedData appendData:data]; long long resourceLength = [receivedData length];
float res = [receivedData length]; percent = res/exp; [progress setHidden:NO]; [progress setProgress:percent]; NSLog(@「Remaining:%lli KB」,(expectedBytes-resourceLength)/ 1024); [kbleft setHidden:NO]; [kbleft setText:[NSString stringWithFormat:@「%lli /%lli KB」,expectedBytes/1024,(resourceLength)/ 1024]]; }
在connectiondidfinish裝載
我關閉文件。所有工作正常幾乎所有的主機除了主機之外,其中有一個捕獲程序之前像filedude.com 在uiwebview我可以衝浪到下載頁面輸入驗證碼,並獲得下載鏈接。當我點擊它時,將在文件目錄中創建文件,文件名和下載開始,但他沒有得到任何數據。每個文件都有0kb和NSLog(@「content-length:%lli Bytes」,expectedBytes);給出了100-400字節之類的東西。
有人可以幫我解決這個問題嗎?
親切的問候
到其他filehosters沒有什麼區別;( – Tammo 2011-01-09 23:31:13