Reputation: 3378
I've been trying to work on this for days now. I've done a lot of research and have tried many other things, but nothing seems to work. Posted is the code that seems like it is the closest to working. All of the data populates in the database except for the image. What I am trying to gain from this is to be able to post the exact data that is returned by UIImagePNGRepresentation(image). I assume that this is binary data, but am not 100%. Any help would be appreciated. Thanks!
-(void)sendFormData:(NSArray*)data{
// add boundary
NSString *boundary=@"----Boundary+AaB03x";
// create array of php vars
NSArray *phpVariables=[NSArray arrayWithObjects:@"username",@"picture",@"firstName",@"lastName",@"phone",@"email",@"password",nil];
// create body data
NSMutableData *body=[[NSMutableData alloc] init];
// dictionary of fields
NSMutableDictionary *formFields=[[NSMutableDictionary alloc] init];
// fill dictionary
for(int i=0; i<[data count]-1; i++) [formFields setObject:[data objectAtIndex:i] forKey:[phpVariables objectAtIndex:i]];
NSMutableURLRequest *urlRequest=[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"MY_URL"]];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:[NSString stringWithFormat:@"multipart/form-data, boundary=%@",boundary] forHTTPHeaderField:@"Content-Type"];
NSEnumerator *dictionaryEnumerator=[formFields keyEnumerator];
NSString *currentKey=nil;
while((currentKey=[dictionaryEnumerator nextObject])){
id currentValue=[formFields objectForKey:currentKey];
if([currentValue class]==[UIImage class]){
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"myphoto.png\"\r\n",currentKey] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/x-png\r\n" dataUsingEncoding:NSASCIIStringEncoding]];
[body appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:UIImagePNGRepresentation(currentValue)];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
}
else{
[body appendData:[[NSString stringWithFormat:@"--%@\r\nContent-Disposition: form-data; name=\"%@\"\r\n\r\n%@\r\n",boundary,currentKey,currentValue] dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO]];
}
}
[urlRequest setHTTPBody:body];
NSURLResponse *response;
NSError *error;
NSData *serverResponseData=[NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&response
error:&error];
[body release];
[formFields release];
NSString *serverResponseString=[[NSString alloc] initWithData:serverResponseData
encoding:NSUTF8StringEncoding];
[self performSelectorOnMainThread:@selector(showServerResponseWithString:)
withObject:serverResponseString
waitUntilDone:FALSE];
}
UPDATE:
The code above works fine and it was actually an issue with the PHP.
Upvotes: 1
Views: 709
Reputation: 1167
I recommend to use ASIFormDataRequest for such things (https://github.com/pokeb/asi-http-request/tree). It allows to avoid such low level request constructing when it's easy to make a little mistake which makes your request invalid
Upvotes: 1