如何在NSMutableURLRequest POST中包含文本字段值

时间:2012-05-23 20:29:50

标签: ios xcode nsmutableurlrequest

我正在开发图片上传应用。我让上传部分工作,并希望在POST中包含来自UITextField的评论。

UITextField名为commentTextmyURL在viewDidLoad中定义。

- (IBAction)startUpload {

uploadButton.enabled = NO;
NSData *imageData = UIImageJPEGRepresentation(imageView.image, 0.7);

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:myURL]];
[request setHTTPMethod:@"POST"];

//Trying to add comment from uitextfield commentText
NSLog(@"Comment Text: %@", [commentText text]);
NSString *comment = [NSString stringWithFormat:@"comment=%@", [commentText text]];

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

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"test.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

//for comment
[body appendData:[comment dataUsingEncoding:NSUTF8StringEncoding]];
//[body appendData:[NSData dataWithBytes: [comment UTF8String] length: [comment length]]];

NSLog(@"comment: %@", comment);
[request addValue:[NSString stringWithFormat:@"%i", [body length]] forHTTPHeaderField:@"Content-Length"];

[request setHTTPBody:body];

NSURLResponse *response;
NSError *error;

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"responseData: %@", responseData);
}

这些代码大部分都是从我读过的不同帖子拼凑而成的,老实说,我还不知道有关边界的任何信息。我试过改变闭合边界,但没有区别。使用PHP我已经检查了$ _POST,$ _GET和$ _REQUEST值,但评论不存在(它至少会显示在$ _REQUEST中,不是吗?)。我知道还有其他相同的问题(http://stackoverflow.com/questions/9509419/xcode-easiest-way-to-send-data-from-ios-textfield-for-example-to-a-remote- datab),我读过,但我似乎无法弄明白。

谢谢,

标记

编辑:我试图添加的评论是否需要再次由边界分隔,并且需要为其声明内容类型?或者沿着这些方向做什么?

1 个答案:

答案 0 :(得分:0)

我将各种字段和音频数据添加到我的POST中,具体如下:

  NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@", BASE_URL]];
  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

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

  NSMutableData *requestData = [NSMutableData data];

  for (NSString *key in [voicenoteData allKeys]) {
    if (![key isEqualToString:@"audio_data"]) {            
      [requestData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
      [requestData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
      [requestData appendData:[[NSString stringWithFormat:@"%@\r\n", [voicenoteData objectForKey:key]] dataUsingEncoding:NSUTF8StringEncoding]];
    }
  }

  [requestData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  [requestData appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"audio_file\"; filename=\"recording.m4a\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  [requestData appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  [requestData appendData:[voicenoteData objectForKey:@"audio_data"]];
  [requestData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];


  request.HTTPBody = requestData;