Submit images as part of form data from iPhone

I managed to get plain text data sent to the 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 this I can send text form data. But how can I send PNG images?

+3
source share
2 answers

You can simply convert the image to an NSData object and then base64 encode it to send it as a parameter.

Base64 encoding examples found here: How to make base64 encoding on iphone-sdk?

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

Source: https://habr.com/ru/post/1774574/


All Articles