0

What i trying to do is send image and text to my database in post method

- (void)uploadpic
{
    NSData *imageData = UIImageJPEGRepresentation(self.imageview.image, 90);
    //   NSString *action=@"tempinsert";

    NSString *s=@"54.316130";
    NSString *s2=@"9.950930";
    NSString *facebook=@"12345";
    NSString *cate=@"1";

    NSString *action=@"insert";

    NSString *note=@"Iiiiiii";


    NSString *str=[NSString stringWithFormat:@"my url.php"];
    NSString *urlString = [NSString stringWithFormat:@"%@",str];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];
    NSMutableData *body = [NSMutableData data];
    NSString *boundary = @"---------------------------14737809831466499882746641449";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Disposition: form-data; name=\"uploaded_file\"; filename=\"popup1.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

    //  parameter username

    //    "lat=%@&long=%@&sdkuserid=%@&cat=%@&action=%@"]; My parameters


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

    [body appendData:[facebook dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];


    //  parameter token
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"insert\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[action dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];


    // parameter method
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"cat\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[cate dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];


    //parameter method

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

    [body appendData:[s2 dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

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


    // setting the body of the post to the reqeust
    [request setHTTPBody:body];


    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    // NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableLeaves error:nil];
    //
    NSLog(@" -------   >%@",returnData);
    NSLog(@" -------   >%@",dict);
}

output

  take photo[1295:31484]  -------   ><0d0a0d0a 0d0a0d0a>
  take photo[1295:31484]  -------   >(null)

My out is always null.I ma trying to send image and few text to server .Although i donnot have any such validation that firld cannot be empty so i have tried with less data.But I get null everytime.

sargeras
  • 179
  • 1
  • 14
sandeep nag
  • 148
  • 8
  • 1
    Check your server log and add error handling into your code – Wain Oct 12 '15 at 06:44
  • Try to upload the image content type [@"Content-Type: image/png\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding] – Gowtham Oct 12 '15 at 06:47
  • Are you expecting any data back from upload API ? I think status code 200 would be enough to confirm upload. Get NSURLResponse and check what status server is responding with. – dev gr Oct 12 '15 at 06:50
  • @sandeep nag My suggestion is that please convert image into `base64` string and then send to server,server logic can convert string to image conversion vice versa. – Nimit Parekh Oct 12 '15 at 07:02
  • try this link to get file and use it. i have uploaded the image using this file https://gist.github.com/igaiga/1354221 – HariKrishnan.P Oct 12 '15 at 10:53
  • with out base 64 how can i do it .and I am also not getting any error – sandeep nag Oct 12 '15 at 11:09

1 Answers1

0
//Make sure you are sending a valid Base64 image data.    
NSData *data = UIImageJPEGRepresentation(imageToSend,1);
    NSString *imageData = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

//Make a request
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSError *error;
            NSData *imageJsonData = [NSJSONSerialization dataWithJSONObject:dictionaryWithImageDataStringAndSomeText
                                                                    options:NSJSONWritingPrettyPrinted
                                                                      error:&error];
            NSString *urlString = yourImageUploadUrlToSomeServer;
            NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
            [request setURL:[NSURL URLWithString:urlString]];
            [request setHTTPMethod:@"POST"];
            [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
            [request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)imageJsonData.length] forHTTPHeaderField:@"Content-Length"];
            [request setHTTPBody:imageJsonData];

            NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
            NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

            NSLog(@"Image upload response: %@",returnString);
        });

//At your server decode back your image data, if you're using java do something like this:
byte[] data = Base64.decode(imageData);
InputStream in = new ByteArrayInputStream(data);
BufferedImage bi = ImageIO.read(in);
ImageIO.write(bi, "jpg", new File(to/some/path))
Atif Imran
  • 1,899
  • 2
  • 18
  • 33