【问题标题】:How to get Upload time remaining from Asynchronous Request?如何从异步请求中获取剩余上传时间?
【发布时间】:2013-02-28 13:58:52
【问题描述】:

我正在尝试将图像从 iPhone 上传到服务器。我可以这样做但我需要显示一个指标。我的代码是

NSData *imageData = UIImageJPEGRepresentation(newImage, 90);

// setting up the URL to post to
NSString *urlString = [NSString stringWithFormat:@"URL",[[formater stringFromDate:now] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
// setting up the request object now
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
  NSString *boundary = [NSString stringWithFormat:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
 NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"ipodfile.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];

这是触发请求的代码。

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                               NSLog(@"Finished with status code: %i", [(NSHTTPURLResponse *)response statusCode]);
 }];

那么我如何计算剩余时间以便显示指标? 感谢您的宝贵时间。

【问题讨论】:

标签: iphone ios progress-bar image-uploading


【解决方案1】:

您可以使用这种连接委托的方法:

- (void)       connection:(NSURLConnection *)connection
          didSendBodyData:(NSInteger)bytesWritten
        totalBytesWritten:(NSInteger)totalBytesWritten
totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite;

(我应该如何包装这么长的参数名称?)
它不在NSURLConnectionDataDelegate 的文档中,但如果您检查公共标头NSURLConnection.h,它就在那里。所以不用担心使用它。

来自文档URL 加载系统编程指南 > 使用 NSURLConnection

估计上传进度

您可以使用 connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite: 委托方法估计 HTTP POST 上传的进度。请注意,这不是对上传进度的精确测量,因为连接可能会失败或连接可能会遇到身份验证挑战。

【讨论】:

  • 嗨@iMartin 感谢您的回答。我已经将此添加到我的代码中但此方法没有被调用。我也在 .h 文件中添加了NSURLConnectionDelegate。是由于以下代码吗? [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { NSLog(@"Finished with status code: %i", [(NSHTTPURLResponse *)response statusCode]); }];
  • 是的,您将不得不使用其他方法,因为您需要设置委托。例如使用–initWithRequest:delegate:,然后使用-start。然后实现一些其他的委托方法来检查完成或错误。
【解决方案2】:

我相信您想显示进度条之类的东西。我建议使用 MKNetworkKit,它为您提供了许多网络管理工具,例如异步连接的进度。检查这个link

【讨论】:

  • 嗨@CainaSouza 感谢您的回复。我不想为这个小任务使用任何第三方库。该套件对于复杂的任务很有用。
猜你喜欢
  • 2016-07-10
  • 1970-01-01
  • 2013-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-26
  • 2011-01-17
  • 1970-01-01
相关资源
最近更新 更多