cannyboy
cannyboy

Reputation: 24426

Sending images as part of form data from an iPhone

I've managed to get simple text data sent to a server using this code:

NSMutableString *parameterString = [[NSMutableString alloc] initWithString: @""];
[parameterString appendString:@"name=steve&"];
[parameterString appendString:@"surname=jobs&"];
[parameterString appendString:@"age=55"];
NSURL *url = [NSURL URLWithString:@"http://example.come/script/"];
request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
NSData *parameterData = [parameterString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:parameterData];

With that I can send text form data. But how can I send PNG images as well?

Upvotes: 3

Views: 684

Answers (2)

saurb
saurb

Reputation: 685

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addData:imageData withFileName:@"image.png" andContentType:@"image/png" forKey:@"yourParamKey"];

Upvotes: 0

Brad
Brad

Reputation: 11515

You could just convert the image to an NSData object, then base64 encode it to send it as a parameter.

Base64 encoding examples found here: How do I do base64 encoding on iphone-sdk?

Upvotes: 1

Related Questions