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];
}
现在我想添加一些文字到“标题”和“描述”文本框。如何用这种代码做到这一点?
它不起作用。 ;( – scourGINHO
你是怎么添加在后期数据密钥和值在服务器上可用其实我用上传图片的代码与服务器上的内容,那么寄托都正在 –
@scourGINHO ..再次见到我已经改变了我的答案 –