gpt4 book ai didi

iphone - NSURLConnection 发送数据两次

转载 作者:行者123 更新时间:2023-12-03 20:42:52 28 4
gpt4 key购买 nike

我正在将图像上传到我的服务器,当我上传图像时,我试图向用户显示上传进度条。但在 NSURLConnection 委托(delegate)方法 连接中:
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];
}

这是我的委托(delegate)方法:

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

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

最佳答案

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

The value of totalBytesExpectedToWrite may change during the upload if the request needs to be retransmitted due to a lost connection or an authentication challenge from the server.

(NSURLConnection 委托(delegate)方法的文档在从非正式协议(protocol)转变为正式协议(protocol)时似乎已很大程度上丢失。您可以检查 header 中的注释或让 Xcode 下载以下之一旧的文档集并在那里检查。)

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

关于iphone - NSURLConnection 发送数据两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10616597/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com