1

I tried to send an image and some text information with HTTP post, but for some reason the post method doesn't send the image but only the text information.

This is my method.

UIImage *image = self.imgBtnPhoto.imageView.image; //is a photo taken and viewed in button.

NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];

NSString *post = [NSString
                  stringWithFormat:
                  @"author=%@&comment=%@&comment_post_ID=%@&attachment=%@",
                  strUsername,
                  stredtComment,
                  strIDArgument,
                  imageData];



NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"https://myUrl.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

[request setHTTPBody:postData];

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

if(conn) {
    NSLog(@"Connection Successful"); //The post is sended and viewed in my WebSite without image!
} else {
    NSLog(@"Connection could not be made");
}

Where is the mistake?

Regards

Bruno
  • 181
  • 16

2 Answers2

1

you need to set header and boundry for image...check out the below code and tried it out...may work for you..

 //To convert your image file to raw.
NSString *urlStr = [NSString stringWithFormat:@"urlwithparams"];  

NSString *urlString = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  

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

[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];  

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

//Your uploading file.
NSMutableData *bodyData = [NSMutableData data];
[bodyData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];  

/*
 * Declares the PHP received $_FILES variables.
 * - $_FILES['_varName']['name'] = _sample.png
 */

[bodyData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"sample_.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[@"Content-Type: image/png\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

[bodyData appendData:[NSData dataWithData:imageData]];

[bodyData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:bodyData];
NSLog(@"request-->%@",request);

//Received response of server.
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
//Dumps it.
NSLog(@"%@", returnString);
Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
  • Thanks, I use your answer and another solved question http://stackoverflow.com/questions/8564833/ios-upload-image-and-text-using-http-post Now everything is working properly. Thank you very much for the instant reply ;) – Bruno Apr 02 '15 at 13:38
0

You're doing wrong, the HTTP body can't be the image data directly, you should create a body with the headers.

NSString *boundary = @"---------------------------14737809831466499882746641449";
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:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"image\"\r\n"]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:postData];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
Pablo A.
  • 2,042
  • 1
  • 17
  • 27