NSData and Uploading Images via POST in iOS NSData and Uploading Images via POST in iOS ios ios

NSData and Uploading Images via POST in iOS


This code works in my app. If you're not using ARC you'll need to modify the code to release anything alloc'ed.

// create requestNSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];                                    [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];[request setHTTPShouldHandleCookies:NO];[request setTimeoutInterval:30];[request setHTTPMethod:@"POST"];// set Content-Type in HTTP headerNSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];[request setValue:contentType forHTTPHeaderField: @"Content-Type"];// post bodyNSMutableData *body = [NSMutableData data];// add params (all params are strings)for (NSString *param in _params) {    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];    [body appendData:[[NSString stringWithFormat:@"%@\r\n", [_params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];}// add image dataNSData *imageData = UIImageJPEGRepresentation(imageToPost, 1.0);if (imageData) {    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];    [body appendData:[[NSString stringWithString:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];    [body appendData:imageData];    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];}[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];// setting the body of the post to the reqeust[request setHTTPBody:body];// set URL[request setURL:requestURL];


i hope this code will help some body else ...

//create requestNSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];//Set Params[request setHTTPShouldHandleCookies:NO];[request setTimeoutInterval:60];[request setHTTPMethod:@"POST"];//Create boundary, it can be anythingNSString *boundary = @"------VohpleBoundary4QuqLuM1cE5lMwCy";// set Content-Type in HTTP headerNSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];[request setValue:contentType forHTTPHeaderField: @"Content-Type"];// post bodyNSMutableData *body = [NSMutableData data];//Populate a dictionary with all the regular values you would like to send.NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];[parameters setValue:param1 forKey:@"param1-name"];[parameters setValue:param2 forKey:@"param2-name"];[parameters setValue:param3 forKey:@"param3-name"];// add params (all params are strings)for (NSString *param in parameters) {    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];    [body appendData:[[NSString stringWithFormat:@"%@\r\n", [parameters objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];}NSString *FileParamConstant = @"imageParamName";NSData *imageData = UIImageJPEGRepresentation(imageObject, 1);//Assuming data is not nil we add this to the multipart formif (imageData){    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];    [body appendData:[@"Content-Type:image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];    [body appendData:imageData];    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];}//Close off the request with the boundary[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];// setting the body of the post to the request[request setHTTPBody:body];// set URL[request setURL:[NSURL URLWithString:baseUrl]];[NSURLConnection sendAsynchronousRequest:request                                   queue:[NSOperationQueue mainQueue]                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {                           NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;                           if ([httpResponse statusCode] == 200) {                               NSLog(@"success");                           }                       }];


Take a look at Apple's SimpleURLConnections example project. You can use the PostController from there with a few modifications.

However - it's not exactly simple. The difference to the solution above is that Apple's is using a stream to stream the file to the server. That's much more memory-friendly than keeping the encoded image data around for the duration of the upload. It's also way more complicated.