2010-09-27 34 views
0

我正在嘗試下載http://www.vesseltracker.com/earth/vesseltrackerlight.kmz,但沒有得到所有零碎。NSURLConnection只下載第567個字節?

我想:

NSData *data = [NSData dataWithContentsOfURL: serverURL options: 0 error: &error]; 

無濟於事

然後切換到

- (void)startDownloadingURL:(NSURL*) url 
{ 
    // Create the request. 
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:url 
      cachePolicy:NSURLRequestUseProtocolCachePolicy 
      timeoutInterval:60.0]; 

    // create the connection with the request 
// and start loading the data 
NSLog(@"SNNetworkController.startDownloadingURL [%@]", url); 
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
if (theConnection) { 
    // Create the NSMutableData to hold the received data. 
    // receivedData is an instance variable declared elsewhere. 
    receivedData = [[NSMutableData data] retain]; 
    } else { 
     // inform the user that the download failed. 
    NSLog(@"SNNetworkController.startDownloadingURL Download failed!"); 
    } 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    // This method is called when the server has determined that it 
    // has enough information to create the NSURLResponse. 

    // It can be called multiple times, for example in the case of a 
    // redirect, so each time we reset the data. 

    // receivedData is an instance variable declared elsewhere. 
    [receivedData setLength:0]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    // Append the new data to receivedData. 
    // receivedData is an instance variable declared elsewhere. 
    [receivedData appendData:data]; 
} 

- (void)connection:(NSURLConnection *)connection 
    didFailWithError:(NSError *)error 
{ 
    // release the connection, and the data object 
    [connection release]; 
    // receivedData is declared as a method instance elsewhere 
    [receivedData release]; 

    // inform the user 
    NSLog(@"SNNetworkController.didFailWithError Download failed! Error - %@", 
      [error localizedDescription]); 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    // do something with the data 
    // receivedData is declared as a method instance elsewhere 
    NSLog(@"SNNetworkController.downloadDidFinish Succeeded! Received %d bytes of data",[receivedData length]); 

    // release the connection, and the data object 
    [connection release]; 
    [receivedData release]; 
} 

但我的運氣。它總是需要567字節(應該是在4k左右)我認爲它可能開始解壓縮並失敗....

回答

2

我下載了你用Safari列出的URL,它只有567字節長。您是否將「4k」期望放在Finder列表視圖所說的內容上?由於文件分配塊大小,該顯示僅爲近似值......文件的實際字節數顯示在該文件的「Get Info ...」窗口後面的括號內。

+0

我用curl下載了它,得到了相同的結果,所以它不是WebKit-/Foundation特定的東西;看起來服務器提供的文件實際上只有567字節。 – 2010-09-28 07:32:32