视频上传到iOS服务器

时间:2014-02-21 10:00:23

标签: ios iphone objective-c uiimagepickercontroller

我试图将视频上传到服务器。我有两种上传视频的选择。首先是,拍摄视频并将其上传到服务器。第二个是,从照片库中选择视频并将其上传到服务器。

第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];
}

并且委托方法是,

- (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];
}

提出任何想法来解决这个问题。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

//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']);