2013-12-12 50 views
0

我有以下问题..我想视频数据和一些字符串张贴到PHP服务器。我能够发布视频数据,但我不知道如何添加字符串。下面是我使用的代码,它做工精良张贴视频,但现在我要添加文本的帖子。如何将字符串和视频发布到PHP服务器?

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

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

    // Return the post data: 
    return postData; 
} 

-(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://pik.bg/android_test.php"] 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]; 
    NSData *returnData = [NSURLConnection sendSynchronousRequest:uploadRequest returningResponse:nil error:nil]; 
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 
    NSLog (@"%@", returnString); 
} 

    // Button for sending the news to the web. 
-(IBAction)sendNews:(id)sender 
{ 
    NSData *videoData = [NSData dataWithContentsOfFile:videoString]; 
    [self post:videoData]; 
} 

现在我想添加一些文字到“标题”和“描述”文本框。如何用这种代码做到这一点?

回答

1

使用这可能是有益的..

NSString *strURL [email protected]"Your URL"; 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ; 
[request setURL:[NSURL URLWithString:strURL]]; 
[request setHTTPMethod:@"POST"]; 

NSString *boundary = @"---------------------------14737809831466499882746641449"; 
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
[request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 


NSMutableData *body = [NSMutableData data]; 
//Image Uploading 
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"Content-Disposition: form-data; name=\"video_mp4\"; filename=\"movie.mp4\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 

NSData *videoData = @"Your video in data Format"; 
[body appendData:[@"Content-Type: video/mp4\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[NSData dataWithData:videoData]]; 
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 


//Data Sending 
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"Your Key That's need on server\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"Your Data That's you want to send on server" dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
[request setHTTPBody:body]; 

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 
NSLog(@"data Return From Server %@",returnString); 

我已经改变了我的答案。现在根据您的服务器需求来检查和安排。

+0

它不起作用。 ;( – scourGINHO

+0

你是怎么添加在后期数据密钥和值在服务器上可用其实我用上传图片的代码与服务器上的内容,那么寄托都正在 –

+0

@scourGINHO ..再次见到我已经改变了我的答案 –

相关问题