2013-04-11 130 views
-1

在我的应用程序中,我正在iPad上录制视频,并且希望将其上传到我的服务器。请让我知道我如何才能做到这一点。在服务器上传视频

+1

简单的问题和简单的答案是你可以实现这一点与iOS SDK。现在尝试一些代码来解决任何问题,这里的代码肯定会得到解决方案。 – Buntylm 2013-04-11 11:10:26

+0

使用此链接http://stackoverflow.com/questions/9923885/how-to-upload-video-file-to-server-in-iphone-application – 2013-04-11 11:10:44

+0

欢迎来到stackoverflow,你有尝试过什么吗?完成任何研究?你有没有使用Google? (将视频上传到服务器objective-c)。请参阅http://stackoverflow.com/faq,一旦你已经做了一些努力,然后回来问这个问题。 – Popeye 2013-04-11 11:11:19

回答

1

创建您的请求字典在它的文件对象,后来postWith:功能将操纵你的视频结合任务

NSDictionary *requestDict = [NSDictionary dictionaryWithObjectsAndKeys:@"FirstObjectValue",@"FirstKey", 
          @"Second Object",@"Second Key", 
          @"myFileParameterToReadOnServerSide",@"file", nil]; // This line indicate ,POST data has file to attach 
[self postWith:requestDict]; 

下面的函数将要张贴的对象字典阅读所有的参数,如果你想发送视频然后在你的字典中添加“文件”键标识有一些文件要发送请求

- (void)postWith:(NSDictionary *)post_vars 
{ 
#warning Add your Webservice URL here   
    NSString *urlString = [NSString stringWithFormat:@"YourHostString"]; 

    NSURL *url = [NSURL URLWithString:urlString]; 
    NSString *boundary = @"----1010101010"; 

    // define content type and add Body Boundry 
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
    [request setHTTPMethod:@"POST"]; 
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 

    NSMutableData *body = [NSMutableData data]; 
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

    NSEnumerator *enumerator = [post_vars keyEnumerator]; 
    NSString *key; 
    NSString *value; 
    NSString *content_disposition; 

    while ((key = (NSString *)[enumerator nextObject])) { 

     if ([key isEqualToString:@"file"]) { 

      value = (NSString *)[post_vars objectForKey:key]; 
      // ***  Write Your Image Name Here *** 
      // Covnert Video to Data and bind to your post request 
#warning Add your Video From Path 
      NSString *videoFilePath = [[NSBundle mainBundle] pathForResource:@"MyVideo" ofType:@"mp4"]; 
      NSData *postData = [NSData dataWithContentsOfFile:videoFilePath]; 

      [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\";\r\nfilename=\"testUploadFile.mp4\"\r\n\r\n",value] dataUsingEncoding:NSUTF8StringEncoding]]; 
      [body appendData:postData]; 
     } else { 
      value = (NSString *)[post_vars objectForKey:key]; 

      content_disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key]; 
      [body appendData:[content_disposition dataUsingEncoding:NSUTF8StringEncoding]]; 
      [body appendData:[value dataUsingEncoding:NSUTF8StringEncoding]]; 

     } 

     [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

    } 

    //Close the request body with Boundry 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

    [request setHTTPBody:body]; 
    [request addValue:[NSString stringWithFormat:@"%d", body.length] forHTTPHeaderField: @"Content-Length"]; 

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 
    NSLog(@"%@", returnString);   
}