Aabasaheb Deshpande
Aabasaheb Deshpande

Reputation: 399

Image is not getting sent to the PHP web service through POST method in IOS

We have a PHP web service that attaches an image to an email since it is collecting the image in a global file array. The method of transfer of the data is the POST method . I am sending the image but it is not getting collected in the web service with the POST method. Can someone tell me if the problem is in the web service or in my code.

Below is the code i am using to send the image.

 NSString *tempString = [NSString stringWithFormat:@"sub1_fname=%@&sub1_lname=%@&sub1_email=%@&sub1_phone=%@&sub1_address=%@&sub1_city=%@&sub1_zip=%@&sub2_storage=%@&sub2_boxes=%@&sub2_oneman=%@&sub2_twoman=%@&sub2_heavy=%@&sub2_comments=%@&sub3_findus=%@&sub3_word=%@&sub3_praise=%@",f31fname,f31lname,f31email,f31phone,f31address,f31city,f31zip,f32storage,f32boxes,f32oneman,f32twoman,f32heavy,f32comments,f33findus,f33word,f33praise];

UIImage *image = [UIImage imageNamed:@"textlogo.png"];

NSMutableURLRequest *request =[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://ommultiengg.com/form/form3.php"]];
[request setHTTPMethod:@"POST"];

NSString *postString =[NSString stringWithFormat:@"{\"sub2_image\":\"%@\"}",image];//post the image
[request setValue:[NSString
                   stringWithFormat:@"%lu", (unsigned long)[postString length]]
 forHTTPHeaderField:@"Content-length"];//get image length
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue:@"application/x-www-form-urlencoded; charset=utf-8"     forHTTPHeaderField:@"Content-Type"];

[request setHTTPBody:[tempString dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

[connection start];

Upvotes: 1

Views: 335

Answers (1)

iPatel
iPatel

Reputation: 47069

Using following code, you can pass parameters as well as with image data.

NSString *urlString = [NSString stringWithFormat:@"http://myAPIName/MethodName/test.php&username=%@&password=%@&image=%@&answer=%@&question_id=%@", username, password, imageName, answer, questionID];
NSLog(@"MyURL: %@",urlString);

urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];

[request setHTTPMethod:@"POST"];
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]];

NSString *str=[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"SourceImage\"; filename=\"Image_%@\"\r\n",[imagePath lastPathComponent]];
[body appendData:[[NSString stringWithString:str] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithContentsOfFile:imagePath]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:body];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

Upvotes: 2

Related Questions