【问题标题】:Video upload into server in iOS将视频上传到 iOS 中的服务器
【发布时间】:2014-02-21 10:00:23
【问题描述】:

我正在尝试将视频上传到服务器。我有两个选择上传视频。第一个是,拍摄视频并将其上传到服务器。第二个是,从照片库中选择视频并上传到服务器。

第 1 步: 在这种情况下,拍摄视频并将其上传到服务器运行良好。

我得到服务器的响应是“返回字符串:{“jsonstatus”:“ok”,“message”:“感谢您的发帖。”}”。

现在我尝试从服务器获取此视频。在 media_file 中显示“x.x.x.x/video/96b8954fbed797500da708fd7bad2261video.mov” - 我成功播放了这个视频。

第 2 步: 但是当我从照片库中选择视频时,它成功地选择了视频。现在我将此视频发布到服务器。

我得到服务器的响应是“返回字符串:{“jsonstatus”:“ok”,“message”:“感谢您的发帖。”}”。

现在我尝试从服务器获取此视频。在 media_file 中显示“无效文件”

我用来选择视频是,

- (IBAction)act_UploadVideoBtn:(id)sender
{
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie,      nil];

    [self presentViewController:imagePicker animated:YES completion:NULL];
}

而Delegate方法是,

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

//    movieURL = [info valueForKey:UIImagePickerControllerMediaURL];
//    
//    [picker dismissViewControllerAnimated:YES completion:NULL];


    NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];

    if (CFStringCompare ((__bridge CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) {
        NSString *moviePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];
        // NSLog(@"%@",moviePath);
        movieURL=(NSURL*)[info objectForKey:UIImagePickerControllerMediaURL];

        if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath)) {
            UISaveVideoAtPathToSavedPhotosAlbum (moviePath, nil, nil, nil);
        }
    }

    [picker dismissViewControllerAnimated:YES completion:NULL];
}

给出解决这个问题的任何想法。

【问题讨论】:

    标签: ios iphone objective-c uiimagepickercontroller


    【解决方案1】:

    您能描述一下您的服务器端吗?到目前为止,我知道这可能是一个完整的 url 问题。也许你的json的sn-ps?也许查看您对正在写入的文件夹的权限。如果您使用 ubuntu 和 Lamp 作为堆栈(我知道的菜鸟),请确保它是 r+w,但不是 exec。

    【讨论】:

      【解决方案2】:
      //push video path to NSData
      NSData *webData = [NSData dataWithContentsOfURL:movieURL];    
      
      NSURL *url = [NSURL URLWithString:@"http://www.example.com/upload_media.php"];
      NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];  
      [request setHTTPMethod:@"POST"];
      NSString *boundary = @"+++++ABck";
      NSString *contentType = [NSString stringWithFormat:@"multipart/form-data;   
      boundary=%@", boundary];
      [request addValue:contentType forHTTPHeaderField:@"Content-Type"];
      NSMutableData *body = [NSMutableData data];
      
      [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
      [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"YOURFILENAME\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
      [body appendData:[@"Content-Type: video/quicktime\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];//video/quicktime for .mov format
      [body appendData:[NSData dataWithData:webData]];
      [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
      [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary]
      dataUsingEncoding:NSUTF8StringEncoding]];
      [request setHTTPBody:body];
      
      NSURLResponse *response;
      NSError *error;
      
      [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
      NSLog(@"%@",response);
      NSLog(@"%@",error);
      
      
      /*Server code*/
      move_uploaded_file($_FILES["file"]["tmp_name"],$upload_dir['basedir'].'YOUR_DIR']);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-04-10
        • 2012-02-07
        • 2011-10-09
        • 2016-06-09
        • 2011-06-28
        • 1970-01-01
        相关资源
        最近更新 更多