我正在通过 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];
}
当我的应用程序返回后台时,上传会暂停,当它返回前台时,它会继续上传 我的问题是......如果我上传了一个冗长的视频,这需要时间并且手机会自动锁定,之后上传过程无法正常工作,即上传卡住了,我被迫重新启动应用程序
为什么会这样..请帮我提前清除这个谢谢