【问题标题】:Post Image With parametrs in iphone在iphone中发布带有参数的图像
【发布时间】:2014-03-27 06:28:38
【问题描述】:

我正在使用 NSURLConnection 来集成 Web 服务。

我的代码如下:

NSURL *url = [NSURL URLWithString:Sign_Up_URL];
     NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
     NSString *params = [NSString stringWithFormat:@"vimage=%@&@"vFname=%@&vLname=%@&vEmail=%@&vPassword=%@&vDeviceType=%@&vDeviceToken=%@",image_data,txt_first.text,txt_last.text,txt_email.text,txt_password.text,@"IOS",@"safasf423526dgdsgdsg"];
     [urlRequest setHTTPMethod:@"POST"];
     [urlRequest setValue:header_filed forHTTPHeaderField:@"X_API_KEY"];
     [urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];

     [NSURLConnection sendAsynchronousRequest:urlRequest
     queue:[NSOperationQueue mainQueue]
     completionHandler:^(NSURLResponse *response,
     NSData *data,
     NSError *connectionError) {
     // handle response
     [MBProgressHUD hideHUDForView:self.view animated:YES];
     NSDictionary *json_response = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
     NSLog(@"%@",json_response);
     //  [self login_response:json_response];
     }];

但是如何使用此参数上传图片?

Also Refer this

帮助。

【问题讨论】:

  • 您必须在 NSData 中转换您的图像,然后您必须使用您的参数上传该数据。
  • @NiravPatel,感谢您的回复!我已经尝试过转换 NSdata 但没有得到成功的结果。
  • 解决方案请参考stackoverflow.com/questions/11084403/…。可能有用..
  • 您是否收到任何错误或响应?
  • 您是否尝试过使用 ASIHTTPRequest 来发布带有其他参数的图像数据?..@user2893370

标签: ios iphone objective-c nsurlconnection nsurlrequest


【解决方案1】:

请阅读文章:http://nthn.me/posts/2012/objc-multipart-forms.html,并使用 ContentType :Multipart/from-data

供您参考:

NSString *boundary = @"YOUR_BOUNDARY_STRING";
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 stringWithFormat:@"Content-Disposition: form-data; name=\"photo\"; filename=\"%@.jpg\"\r\n", self.message.photoKey] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"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]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"message\"\r\n\r\n%@", self.message.message] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"user\"\r\n\r\n%d", 1] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:body]; 

希望这会有所帮助,如果您遇到任何错误,请更新我。

【讨论】:

    【解决方案2】:

    通过使用 NSURLConnection 你可以像下面那样做

    NSData* fileData = [[NSData alloc] initWithContentsOfFile:image_data];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:Sign_Up_URL]];
    [request setHTTPMethod:@"POST"];
     
    NSString *boundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"]; // This is important! //NSURLConnection is very sensitive to format.
    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",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"VFname\"; filename=\"thefilename\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:fileData]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"param2\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:self.emailtextfield.text] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"param3\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:self.passwordtextfield.text] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:self.vlnametextfield.text] dataUsingEncoding:NSUTF8StringEncoding]];
    // setting the body of the post to the reqeust
    [request setHTTPBody:body];
    

    (或者使用 ASIHTTPRequest 你可以这样做)

        ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:Sign_Up_URL];
        [request setDelegate:self];
    
        [request setPostValue:vnamelabel.textforKey:@"VFname"];
    
        [request setPostValue:emaillabel.text forKey:@"vEmail"];
    
        [request setPostValue:passwordlabel.text forKey:@"vPassword"];
    
        [request setPostValue:devicetypelabel.text forKey:@"vDeviceType"];
    
         [request addData:self.imagedata withFileName:@"image.jpeg" andContentType:@"image/jpeg" forKey:@"vimage"];
         //----------------------
    

    希望对你有帮助..

    【讨论】:

      【解决方案3】:
      猜你喜欢
      • 2017-03-28
      • 1970-01-01
      • 2012-06-04
      • 2017-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多