I am having trouble uploading both a file and additional simple post information when using NSURLConnection. I know there are many questions about this already but none of them seem to answer my question, so i apologise in advance if it has been answered, and if you could point me in the right direction, that would be great.
I am using this code to upload a jpeg chosen form camera roll:
NSData *imageData = UIImageJPEGRepresentation(image, 50);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
[request setTimeoutInterval:10];
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:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@; filename='profileImage.jpg'rn",usernameString] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-streamrnrn" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"rn--%@--rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// now lets make the connection to the web
registerConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
[registerConnection start];
However i also need to send 2 more values as post information such as funcID=1 and username=sampleUsername so my php code knows exactly what to do with the file. I have attempted to appendData to the current body, such as:
[body appendData:[[NSString stringWithFormat:@"funcCode=1&username=%@",
usernameString] dataUsingEncoding:NSUTF8StringEncoding]];
but it doesnt work, and i am not sure why exactly.
Any help is much appreciated, thanks!