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

问题描述:

我想通过web服务将视频上传到服务器。 视频将通过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 

} 

现在我想通过上传按钮动作以邮寄方法服务器选择的视频......

我不得不作出后弦: -

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和else ...

给我代码..

+0

为什么-1谁能告诉me..anyone知道答案 –

张贴视频,您需要图片选择委托后,使用此功能:

- (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"); 
    } 

}