在iphone中上传文件问题

时间:2013-03-13 20:04:54

标签: iphone ios http file-upload

我正在通过HTTP发布将视频上传到服务器。

我正在使用以下代码

 NSString *urlString =@"http://sampleurl.com/upload_video";

 NSMutableURLRequest *request= [[[NSMutableURLRequest alloc] init] autorelease];
 [request setURL:[NSURL URLWithString:urlString]];
 [request setHTTPMethod:@"POST"];


  [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"user_id\"\r\n\r\n%@", appDelegate.userid] dataUsingEncoding:NSUTF8StringEncoding]];
  [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];


   [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"video\"; filename=\"%@\"\r\n", @"a.mov"] dataUsingEncoding:NSUTF8StringEncoding]];
   [postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
   [postbody appendData:[NSData dataWithData:file]];
   [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
   [request setHTTPBody:postbody];

    conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if (conn) {
    webData = [[NSMutableData data] retain];
     }    

这是我用来查找写入的字节数和总字节数

的函数
 -(void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(    NSInteger)totalBytesExpectedToWrite{



    NSLog(@"------%i------%i-----------",totalBytesWritten ,totalBytesExpectedToWrite);



  NSDictionary *uploadStatus=[[NSDictionary alloc]initWithObjectsAndKeys:
                            [NSString stringWithFormat:@"%i",totalBytesWritten],@"bytesWritten",
                            [NSString stringWithFormat:@"%i",totalBytesExpectedToWrite],@"totalBytes",
                            nil];

   [uploadStatus release];
      }

当我的应用程序返回时,地面上传暂停,当它返回到前台时,它会继续上传 我的问题是......如果我上传了一段冗长的视频,那么上传过程无效后需要时间和手机自动锁定,即上传卡住了,我被迫重启应用 < / p>

为什么会发生这种情况..请帮我澄清一下 提前谢谢

1 个答案:

答案 0 :(得分:0)

Apple为应用程序引入了多任务支持。多任务支持依赖于将应用程序置于后台状态,可以将其快速恢复到完全交互模式,但不会在后台消耗资源。这可能会给网络应用程序带来问题,例如那些使用RestKit:构建的重要长期运行请求可能会被用户切换出应用程序中断。值得庆幸的是,Apple还通过使用beginBackgroundTaskWithExpirationHandler UIApplication方法创建后台任务,为延长流程寿命提供了有限的支持。此方法接受Objective-C块并为创建的后台任务返回UIBackgroundTaskIdentifier值。创建后台任务时必须小心,以便保持与iOS 3.0部署的向后兼容性。

请参阅背景上传/下载中的主题

http://mobile.tutsplus.com/tutorials/iphone/advanced-restkit-development_iphone-sdk/链接。

另见http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIApplication_Class/Reference/Reference.html

http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW28

https://developer.apple.com/library/ios/#technotes/tn2277/_index.html

以下行禁用iOS设备的休眠时间。

[[UIApplication sharedApplication] setIdleTimerDisabled:YES]