我必须在ios中创建将视频上传到服务器的应用程序。我正在使用代码上传视频,如下所示。此代码上传小视频并且运行良好但超过5 MB的视频无法上传。
- (void)uploadvideo {
NSString *strurl=[NSString stringWithFormat:@"%@",appDele.DRUPAL_SERVICES_URL];
NSString *url=[[NSString alloc]initWithFormat:@"video_upload.php?user_id=%@&node_id=%@",appDele.UserId,appDele.strProductId];
NSString *urlString =[NSString stringWithFormat:@"%@%@",strurl,url] ;
NSLog(@"%@",urlString);
NSString *encodedString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// setting up the request object now
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:encodedString]];
[request setHTTPMethod:@"POST"];
//[request setTimeoutInterval:10000];
// NSInputStream *videoStream = [[[NSInputStream alloc] initWithData:data1] autorelease];
// [request setHTTPBodyStream:videoStream];
NSString *boundary = [NSString stringWithFormat:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
/*
now lets create the body of the post
*/
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"filename\"; filename=\"New.mp4\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
// NSLog(@"%@New.jpg",appDelegate.MemberId);
[body appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
// [body appendData:data1.length];
[body appendData:[NSData dataWithData:data1]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@",returnString);
}
有关如何在ios中上传大型视频文件的任何建议吗?
答案 0 :(得分:0)
NSURLConnection
已经很老了,看一下NSURLSession
的代表和块。 Here's some tutorial, how to migrate from one to another