0

In my code i am trying to add string and images in server side but strings are getting placed image didn't get stored in to server...

     if (internetStatus != NotReachable)
    {


        //set content type for http request


        NSMutableURLRequest *request=[[NSMutableURLRequest alloc]init];

        [request setURL:[NSURL URLWithString:localRegister]];
        [request setHTTPMethod:@"POST"];
        NSMutableData *body=[NSMutableData data];
        NSString *boundary=@"123456789";
        NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
        [request setValue:contentType forHTTPHeaderField: @"Content-Type"];
//adding first name
        [body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"content-Disposition:form-data;name=\"firstname\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[txtFirstName.text dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//adding email id
       [body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"content-Disposition:form-data;name=\"email\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
       [body appendData:[txtEmailID.text dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//adding telephone number
        NSInteger mobilenumber;
        mobilenumber=[[NSString stringWithFormat:@"%@",txtMobileNo.text] integerValue];
        [body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"content-Disposition:form-data;name=\"telephone\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[txtMobileNo.text dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//adding password
        [body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"content-Disposition:form-data;name=\"password\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[txtPassWord.text dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    if(!imgdata)
        {
            NSLog(@"entering in to image side");
            [body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
            [body appendData:[[NSString stringWithFormat:@"content-Disposition:form-data;name=\"pic\"\r\n\r\n filename=\"image.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
            [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
            [body appendData:[NSData dataWithData:imgdata]];
            [body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        }

        [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [request setHTTPBody:body];
        NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[body length]];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];

Here i am getting a image to upload server side..

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

        //You can retrieve the actual UIImage
        _signupImgView.image = [info valueForKey:UIImagePickerControllerOriginalImage];
        //Or you can get the image url from AssetsLibrary
        NSURL *path = [info valueForKey:UIImagePickerControllerReferenceURL];
        imageSending=[UIImage imageWithData:[NSData dataWithContentsOfURL:path]];
        imgdata = UIImagePNGRepresentation(imageSending);
        [picker dismissViewControllerAnimated:YES completion:^{
        }];
    }

i know some thing wrong in my program please help me to find out my problem.

Kishore Kumar
  • 4,265
  • 3
  • 26
  • 47
  • how is this a multipart related issue? it seems [from the error message you get] that the server can see the fields and only the content is wrong. ask for your server guy what exactly he doesn't like - if you can't. install a proxy and check the value you send against the definion of the fields – Daij-Djan Sep 18 '15 at 11:01
  • voted to close because this is a simple 'why isn't this working' without a proper example or code to reproduce it - I also don't see the research effort here – Daij-Djan Sep 18 '15 at 11:02
  • @Daij-Djan i already asked server side people they said its my fault,so to verify i am post my code if you find out anything wrong please inform me.. – Kishore Kumar Sep 18 '15 at 11:03
  • @Daij-Djan thanks for your response... – Kishore Kumar Sep 18 '15 at 11:05

0 Answers0