【问题标题】:Upload large file video to server in ios将大文件视频上传到ios中的服务器
【发布时间】:2013-11-07 10:14:49
【问题描述】:

我必须在 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 中上传大视频文件有什么建议吗?

【问题讨论】:

  • 你得到了什么错误,改变你的 NSData *returnData = [NSURLConnection sendSynchronousRequest:request returnedResponse:nil error:nil];到 NSError *error;[NSURLConnection sendSynchronousRequest:request returnedResponse:nil error:&error]; if(error!=nil){NSLog(@"%@",error)},您还评论了 setTimeoutInterval 行,我认为 iosversions>=ios6 的默认值为 20 秒, 的默认值为 240 秒

标签: ios iphone


【解决方案1】:

NSURLConnection 已经很老了,看看NSURLSession 的代表和块。 Here's some tutorial, how to migrate from one to another.

【讨论】:

  • NSURLSession 仅在 iOS 7 上受支持,因此,虽然 NSURLConnection 可能“相当陈旧”,但如果要支持尚未升级到 iOS 7 的 ~%25 设备,则它是必要的。帮助使用 NSURLConnection 将不胜感激。
最近更新 更多