Skip to content

Instantly share code, notes, and snippets.

@scottsappen
Created January 24, 2013 20:30
Show Gist options
  • Save scottsappen/4627365 to your computer and use it in GitHub Desktop.
Save scottsappen/4627365 to your computer and use it in GitHub Desktop.
This is a code snippet that uploads a binary image file from an iOS device to a listening RESTful web service that answers to POST. Dispatch this method in a separate thread.
(void)uploadMyNewPictureToWeb {
//your web service that's on the lookout for the image
NSMutableString *customWebPage = [NSMutableString stringWithString:@"your web service URL would go here"];
NSURL *aUrl = [NSURL URLWithString:customWebPage];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl];
//this is your POST, PUT, whatever restful protocol you support
[request setHTTPMethod:@"POST"];
//set the content type
//the spec calls for a unique string of characters as a separator, so use something random, but unique
//also, I can't stress enough how important it is to get all the \r\n's correct
NSString *boundary = @"DK39FJK4589FDKJSW893JKLR89DFJK238934IOSO";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
//body
NSMutableData *body = [NSMutableData data];
//add all the textual params first (I'm going to assume you have some things to transmit other than just a photo
//let's call them param1 and param2
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", @"param1"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", _param1] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", @"param2"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", _param2] dataUsingEncoding:NSUTF8StringEncoding]];
//now add the image data, remember in the original post, I used a button, you could get this from local documents storage or wherever
NSData *imageData = UIImagePNGRepresentation(_profilePictureButton.imageView.image);
if (imageData) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"mrkangaroo.png\"\r\n", @"mrkangaroo"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//set the body to the request
[request setHTTPBody:body];
//set the content length
NSString *postLength = [NSString stringWithFormat:@"%d", [body length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
NSURLResponse *response;
NSError *error;
//send it and do something with the response...response parsing is left off here for simplicity
NSData *myJSONNetworkData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
//parsing response...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment