Skip to content

Instantly share code, notes, and snippets.

@tompave
Last active December 22, 2015 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tompave/6498740 to your computer and use it in GitHub Desktop.
Save tompave/6498740 to your computer and use it in GitHub Desktop.
#define ENDPOINT_URL @"http://www.myawesomeserver.com/data/images"
// elsewhere: we will need this to send the request asynchronously
self.httpQueue = [[NSOperationQueue alloc] init];
/**
* upload method
*/
- (void)uploadImage:(UIImage*)image withImageName:(NSString*)imageName andParams:(NSDictionary*)paramsDict
{
// creating a NSMutableURLRequest that we can manipulate before sending
NSURL *theURL = [NSURL URLWithString:ENDPOINT_URL];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:theURL];
// setting the HTTP method
[request setHTTPMethod:@"POST"];
// we want a JSON response
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
// the boundary string. Can be whatever we want, as long as it doesn't appear as part of "proper" fields
NSString *boundary = @"qqqq___winter_is_coming_!___qqqq";
// setting the Content-type and the boundary
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
// we need a buffer of mutable data where we will write the body of the request
NSMutableData *body = [NSMutableData data];
// writing the basic parameters
for (NSString *key in paramsDict) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", [paramsDict objectForKey:key]] dataUsingEncoding:NSUTF8StringEncoding]];
}
// creating a NSData representation of the image
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
NSString *fileNameStr = [NSString stringWithFormat:@"%@.jpg", imageName];
// if we have successfully obtained a NSData representation of the image
if (imageData) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"photo\"; filename=\"%@\"\r\n", fileNameStr] 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]];
}
else
NSLog(@"no image data!!!");
// we close the body with one last boundary
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// assigning the completed NSMutableData buffer as the body of the HTTP POST request
[request setHTTPBody:body];
// send the request
[NSURLConnection sendAsynchronousRequest:request
queue:self.httpQueue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
NSLog(@"completion handler with response: %@", [NSHTTPURLResponse localizedStringForStatusCode:[(NSHTTPURLResponse*)response statusCode]]);
NSLog(@"response: %i",[(NSHTTPURLResponse*)response statusCode]);
NSInteger status = [(NSHTTPURLResponse*)response statusCode];
if(error){
NSLog(@"http request error: %@", error.localizedDescription);
// handle the error
}
else{
if (status == 201)
// handle the success
else
// handle the error
}
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment