2012-05-16 49 views
2

我正在上傳圖像到我的服務器上傳圖片時,我試圖向用戶顯示上傳的進度條。但是在NSURLConnection委託方法connection: didSendBodyData: totalBytesWritten: totalBytesExpectedToWrite:中,我收到了令人困惑的數字。可以說我的數據是X字節長。起初,我得到totalBytesExpectedToWrite爲X,但是在totalBytesWritten達到totalBytesExpectedToWrite後,我的連接仍然繼續,上傳和totalBytesExpectedToWrite移至2X。它總是2X,我不知道爲什麼。連接是相同的(由id),我只調用一次。NSURLConnection發送數據兩次

這裏是我的圖片上傳代碼:

- (void)uploadImage:(UIImage *)image 
{ 
// random boundary string 
NSString *boundary = @"----8745633765875256----"; 

NSData *imageData = UIImageJPEGRepresentation(image, 1.0); 
NSMutableData *requestData = [[NSMutableData alloc] init]; 

// append boundary and separate it with new line 
[requestData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

// setting up header files, appending image and separating body with boundary 
[requestData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"ad_image\"; filename=\"%@\"\r\n", @"tmp.jpg"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[requestData appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[requestData appendData:imageData]; 
[requestData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

// initializing request 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@&user_id=%@&api_key=%@", kUrlImageTempAdd, userID, apiKey]]]; 
[request setHTTPMethod:@"POST"]; 

// setting up content-type "form" to multipart/form-data for image upload 
[request addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPBody:requestData]; 

//setting up content length 
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content- 

// connection init and start 
NSURLConnection *connection = [[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:true] autorelease]; 
} 

,這裏是我的委託方法:

- (void)connection:(NSURLConnection *)connection 
didSendBodyData:(NSInteger)bytesWritten 
totalBytesWritten:(NSInteger)totalBytesWritten 
totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite 
{ 
NSLog(@"connection %@ bytes sent %d/%d (%f%%)", connection, totalBytesWritten, totalBytesExpectedToWrite, ((float)totalBytesWritten/(float)totalBytesExpectedToWrite) * 100.0f); 
} 

任何建議,爲什麼會這樣?我失去了我的心在這裏:)

+0

發佈日誌還... –

回答

3

-connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:文檔說

totalBytesExpectedToWrite的價值如果 由於連接丟失或 au而需要重傳請求來自服務器的認證挑戰。

(爲NSURLConnection委託方法的文檔似乎當它被非正式到正式的協議轉變在很大程度上失去了,你可以查看評論在標題或具有Xcode的下載老docsets之一,請在那裏查看。)

您可以實施一些其他委託方法,以觀察可能發生的情況,如身份驗證質詢。

+0

不知道我是否應該回答自己的問題或將其標記爲答案。但這就是發生了什麼事。我正在將圖像上傳到受密碼保護的服務器。由於該上傳需要做兩次(不知道爲什麼)。只要我取消了通行保護,它就開始正常工作了! – paxx

1

您使用可叫了很多次,因爲它報告的數據上傳到服務器狀態更新的委託方法...

的NSURLConnection的對象將以字節數據包的形式將數據傳輸到服務器,每次發送數據包時都會調用委託方法。

如果你只是想知道什麼時候上傳並結束了,你需要使用此委託方法:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 

See more in the official documentation of the NSURLDownloadDelegate delegate

+0

好的,但是我怎樣才能更新我的進度條呢?任何這並不能解釋爲什麼它總是2X。不管用戶使用多大的圖像(文件)? – paxx

+0

那麼,你需要使用你使用的委託。但即使它被多次調用,totalBytesWritten應該是您應該用來更新進度條的那個 – Lefteris

+0

它應該被多次調用,因爲數據是以塊的形式發送的,但對我來說很奇怪的是(在同一個連接中)totalBytesExpectedToWrite更改。就好像包裹的大小發生了變化。我會檢查服務器是否可能基於@Ken Thomases反饋請求重新發送(因爲身份驗證質詢) – paxx