【问题标题】:NSURLConnection sends data twiceNSURLConnection 发送数据两次
【发布时间】:2012-05-23 21:37:33
【问题描述】:

我正在将图片上传到我的服务器,在上传图片时,我正在尝试向用户显示上传进度条。但是在 NSURLConnection 委托方法connection: didSendBodyData: totalBytesWritten: totalBytesExpectedToWrite: 中,我得到了令人困惑的数字。假设我的数据是 X 字节长。起初我得到totalBytesExpectedToWrite 为X,但在totalBytesWritten 达到totalBytesExpectedToWrite 之后,我的连接仍然在进行上传,并且totalBytesExpectedToWrite 转移到2X。它总是正好是 2X,我不知道为什么。连接是相同的(按 id),我只调用它一次。

这是我上传图片的代码:

- (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);
}

有什么建议为什么会发生这种情况?我在这里失去理智:)

【问题讨论】:

    标签: iphone objective-c image upload nsurlconnection


    【解决方案1】:

    -connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite: 的文档说

    totalBytesExpectedToWrite 的值可能会在上传过程中发生变化,如果 由于连接丢失或 来自服务器的身份验证质询。

    NSURLConnection 委托方法的文档在从非正式协议转换为正式协议时似乎已大量丢失。您可以检查标头中的 cmets 或让 Xcode 下载一个较旧的文档集并检查在那里。)

    您可以实现一些其他委托方法来观察可能发生的情况,例如身份验证质询。

    【讨论】:

    • 不确定我是应该回答我自己的问题还是将这个问题标记为答案。但这就是发生的事情。我正在将图像上传到受密码保护的服务器。因为那个上传需要做两次(不知道为什么)。一旦我删除了密码保护,它就开始正常工作了!
    【解决方案2】:

    您使用的委托方法可以被调用很多次,因为它会报告数据上传到服务器的状态更新...

    NSURLConnection 对象会以字节包的形式将数据传输到服务器,并且每次发送一个包时它都会调用委托方法。

    如果您只想知道上传何时结束,您需要使用此委托方法:

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    

    See more in the official documentation of the NSURLDownloadDelegate delegate

    【讨论】:

    • 好的,但是我怎样才能更新我的进度条呢?任何这并不能真正解释为什么它总是 2X。我使用的图像(文件)大小无关紧要?!
    • 好吧,那么您需要使用您正在使用的委托。但即使它被多次调用,totalBytesWritten 也应该是你应该用来更新进度条的那个
    • 它应该被多次调用,因为数据是分块发送的,但对我来说奇怪的是(在同一个连接中)totalBytesExpectedToWrite 发生了变化。好像包裹的大小发生了变化。我将检查服务器是否可能根据@Ken Thomases 反馈请求重新发送(因为身份验证挑战)
    猜你喜欢
    • 2021-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-08
    • 2023-03-22
    • 2017-09-13
    • 1970-01-01
    相关资源
    最近更新 更多