将单个视频文件上传到服务器

时间:2015-07-11 06:56:40

标签: ios objective-c xcode web-services

我想通过webservice将视频上传到服务器。 视频将由uiimagepicker拍摄 这里是uiimagepicker的代码: -

- (IBAction)btn_select:(id)sender
{
UIImagePickerController *imgpicker=[[UIImagePickerController alloc]init];
imgpicker.delegate=self;
imgpicker.sourceType=UIImagePickerControllerSourceTypeSavedPhotosAlbum;
imgpicker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie,nil];
[self presentViewController:imgpicker animated:YES completion:nil];}



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

[picker dismissViewControllerAnimated:YES completion:nil]; // dismiss image picker view controller

NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{
    NSURL *mediaUrl = [info objectForKey:UIImagePickerControllerMediaURL];

    MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:mediaUrl];
    moviePlayer.shouldAutoplay = NO;
    NSLog(@"%@",mediaUrl);

    UIImage *thumbnail = [moviePlayer thumbnailImageAtTime:0.0 timeOption:MPMovieTimeOptionNearestKeyFrame];
    [img_out setImage:thumbnail];  //imageView is a UIImageView

}

现在我想通过post方法将所选视频按钮操作上传到服务器...

我为帖子制作了字符串: -

NSString *post=[NSString stringWithFormat:@"uid=791986&category=Game&description=asd&language=English&country=US&bobltags=asd&video=file:///Users/mymac/Library/Developer/CoreSimulator/Devices/3DD070C1-71DE-4672-BAC2-9B4E64F57D0A/data/Containers/Data/Application/68E251CB-7C67-4559-A13D-91F222B9D0EE/tmp/trim.9467B9DE-D7BF-43BE-99DA-366589746063.MOV"];

现在该做些什么。我看了很多例子,但没有正确理解关于kBoundry和其他......

给我代码..

1 个答案:

答案 0 :(得分:1)

用于发布视频,您需要在图像选择器委托之后使用此功能:

- (NSData *)generatePostDataForData:(NSData *)uploadData
{
    // Generate the post header:
    NSString *post = [NSString stringWithCString:"--AaB03x\r\nContent-Disposition: form-data; name=\"upload[file]\"; filename=\"somefile\"\r\nContent-Type: application/octet-stream\r\nContent-Transfer-Encoding: binary\r\n\r\n" encoding:NSASCIIStringEncoding];

    // Get the post header int ASCII format:
    NSData *postHeaderData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    // Generate the mutable data variable:
    NSMutableData *postData = [[NSMutableData alloc] initWithLength:[postHeaderData length] ];
    [postData setData:postHeaderData];

    // Add the image:
    [postData appendData: uploadData];

    // Add the closing boundry:
    [postData appendData: [@"\r\n--AaB03x--" dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];

    // Return the post data:
    return postData;
}


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

    //assign the mediatype to a string 
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

    //check the media type string so we can determine if its a video
    if ([mediaType isEqualToString:@"public.movie"]){
        NSLog(@"got a movie");
        NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
        NSData *webData = [NSData dataWithContentsOfURL:videoURL];
        [self post:webData];
        [webData release];

    }

用于发布视频使用此功能:

- (void)post:(NSData *)fileData
{

    NSLog(@"POSTING");

    // Generate the postdata:
    NSData *postData = [self generatePostDataForData: fileData];
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

    // Setup the request:
    NSMutableURLRequest *uploadRequest = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.example.com:3000/"] cachePolicy: NSURLRequestReloadIgnoringLocalCacheData timeoutInterval: 30 ] autorelease];
    [uploadRequest setHTTPMethod:@"POST"];
    [uploadRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [uploadRequest setValue:@"multipart/form-data; boundary=AaB03x" forHTTPHeaderField:@"Content-Type"];
    [uploadRequest setHTTPBody:postData];

    // Execute the reqest:
    NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:uploadRequest delegate:self];
    if (conn)
    {
        // Connection succeeded (even if a 404 or other non-200 range was returned).
        NSLog(@"sucess");
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Got Server Response" message:@"Success" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
    else
    {
        // Connection failed (cannot reach server).
        NSLog(@"fail");
    }

}