Hey guys, so question I am really stuck on. I have an application that successfully uploads some text values from an application to a server I have set up using a php file. However, now what I want to do is upload a image along with the text. I have the code written to upload the image however, when ever the "send" button is pressed the app sends two different sets of data (one with the text values and one with the image) because I am creating to different connections my question is how do I combine the two.
All the text I have to be uploaded is in an NSString as shown below:
then to send the information I am using:
Now for the image I convert it to a type NSMutableData and break it apart and establish another connection because I am not sure to combine NSMutableData types with NSString's. The code for sending the image is below:
Thanks a lot for any help.
All the text I have to be uploaded is in an NSString as shown below:
Code:
NSString *postData = [NSString stringWithFormat:@"name=%@&age=%d&sex=%d",inputName.text,[inputAge.text intValue],gender.selectedSegmentIndex];
then to send the information I am using:
Code:
[req setHTTPBody:[postData dataUsingEncoding:NSASCIIStringEncoding]];
NSData *returnData = [NSURLConnection sendSynchronousRequest:req returningResponse:nil error:nil];
Now for the image I convert it to a type NSMutableData and break it apart and establish another connection because I am not sure to combine NSMutableData types with NSString's. The code for sending the image is below:
Code:
NSData *imageData = UIImageJPEGRepresentation(imageView.image, 90);
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[req addValue:contentType forHTTPHeaderField:@"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-date; name=\"userfile\"; filename=\".jpg\"r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"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]];
[req setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:req returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
Thanks a lot for any help.