I have the following problem.. I am trying to post video data and some strings to a PHP server. I am able to post the video data, but I don't know how to add strings. Here's the code I am using, it works excellent for posting video, but now I have to add text to the post.
- (NSData *)generatePostDataForData:(NSData *)uploadData
{
// Generate the post header:
NSString *post = [NSString stringWithCString:"--AaB03x
Content-Disposition: form-data; name=\"video_mp4\"; filename=\"movie.mp4\"
Content-Type: video/mp4
Content-Transfer-Encoding: binary
" 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: [@"
--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];
}
Now I want to add some text to "title" and "description" text boxes. How to do that with this kind of code?
Use This may be helpfull ..
NSString *strURL =@"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:@"--%@
", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"video_mp4\"; filename=\"movie.mp4\"
" dataUsingEncoding:NSUTF8StringEncoding]];
NSData *videoData = @"Your video in data Format";
[body appendData:[@"Content-Type: video/mp4
" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:videoData]];
[body appendData:[[NSString stringWithFormat:@"
"] dataUsingEncoding:NSUTF8StringEncoding]];
//Data Sending
[body appendData:[[NSString stringWithFormat:@"--%@
", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"Your Key That's need on server\"
"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Your Data That's you want to send on server" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"
" 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);
I have changed my answer. Now check and arrange according your server need.