1

ライブサーバーにビデオをアップロードしたいので、plzはそれを行う方法を教えてくれます。コードとチュートリアルはより良くなります。また、データを保存するためにサーバー上にフォームを作成する必要がありますか?

データの保存については何も知りません。

データをアップロードするためのこのコード:

- (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];
        webData = [NSData dataWithContentsOfURL:videoURL];
        NSLog(@"webdata is: %@ ",webData);
    }

    [self dismissModalViewControllerAnimated:NO];

    // Handle a movie capture
    if (CFStringCompare ((__bridge_retained CFStringRef) mediaType, kUTTypeMovie, 0)
        == kCFCompareEqualTo) {

        NSString *moviePath = [[info objectForKey:
                                UIImagePickerControllerMediaURL] path];
        if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath)) {
            UISaveVideoAtPathToSavedPhotosAlbum (moviePath,self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
        } 
    }
}

- (void)video:(NSString*)videoPath didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo
{



    if (error) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Video Saving Failed"  delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil, nil];
        [alert show];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Video Saved" message:@"Saved To Photo Album"  delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
        [alert show];
    }
}

- (IBAction)uploadVideo:(UIButton*)sender {
    [self post:webData];
    NSLog(@"posted data %@",webData);
}


- (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:SERVER_PATH] cachePolicy: NSURLRequestReloadIgnoringLocalCacheData timeoutInterval: 30 ];
    [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];
    }
    else
    {
        // Connection failed (cannot reach server).
        NSLog(@"fail");
    }

}

- (NSData *)generatePostDataForData:(NSData *)uploadData
{
    // Generate the post header:
    NSString *post = [NSString stringWithCString:"--AaB03x\r\nContent-Disposition: form-data; name=\"file\"; filename=\"ios-video.mov\"\r\nContent-Type: video/mov\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 file:
    [postData appendData: uploadData];

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

    // Return the post data:
    return postData;
}

そして、私はこのように私のサーバーパスを定義しましたそれは大丈夫ですか?

#define SERVER_PATH @"http://manektech.net/diliptest/"

ログに記録すると、Webデータの出力は次のようになります。

webdata is: <00000014 66747970 71742020 00000000 71742020 00000008 77696465 0003837f 6d646174 00cc4007 00ec99ee d49b2f4d b44830a9 915555df adeb095c  ...............>
4

2 に答える 2

0

ここで、カメラから vedio を取得し、その時点でそれを停止した後、.h ファイルに videoData 名のオブジェクトを作成しNSDataます。imagePickerControllerNSData

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    { 
        NSLog(@"%@",info);
        [self  dismissModalViewControllerAnimated:YES];

        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",[info objectForKey:UIImagePickerControllerMediaURL]]];

        videoData = [[NSData alloc] initWithContentsOfURL:url];
    }

その後、以下のメソッドのようなデータを送信できます..コードで使用するフローに従ってください...

-(void)AddVideo{
    NSString *strUSerID = [NSString stringWithFormat:@"%d",userID ];// if required

    NSString *filename = @"massage.mp4";
    NSString *boundary = @"----BOUNDARY_IS_I";
    NSURL *url;
    //&user_id=1&cat_id=1&dap_title=test113434&origin=hi&todap=test343&nottodap=testtt
    NSString *str = SERVER_PATH; // here put your server URL with insert api
    url = [NSURL URLWithString:str];
    [str release];

    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];

    [req setHTTPMethod:@"POST"];

    NSString *contentType = [[NSString alloc]initWithFormat:@"multipart/form-data;boundary=%@", boundary];

    [req setValue:contentType forHTTPHeaderField:@"Content-type"];

    [contentType release];

    NSMutableData *postBody = [NSMutableData data];


    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"video\"; filename=\"%@\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[@"Content-Type: video/mp4\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:videoData];// use here webData which you use for store vedio data
    [req setHTTPBody:postBody];

    NSURLResponse* response;
    NSError* error;
    NSData* result = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];
    NSString * rsltStr = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
    NSMutableDictionary *dict;
    if(rsltStr == nil){

    }
    else{
        SBJSON *json = [[SBJSON alloc] init];
        NSError *error = nil;
        dict = [json objectWithString:rsltStr error:&error];
        NSLog(@"%@",dict);
        [json release];
    }
    [rsltStr release];  

    [videoData release];
}

この回答がお役に立てば幸いです..

于 2012-10-31T06:15:15.883 に答える
0

TCP ソケット/ストリーム経由で実行する場合は、NSStream API を使用してください。- このメソッドで NSOutputStream を作成し、バッファを書き込みます。

HTTP 経由で実行する場合は、NSURLConnectionを使用してください。HTTPBodyStream でデータを POST として送信します。

于 2012-10-31T06:01:27.633 に答える