Skip to content

Instantly share code, notes, and snippets.

@shiftregister-vg
Created February 23, 2012 21:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shiftregister-vg/1895074 to your computer and use it in GitHub Desktop.
Save shiftregister-vg/1895074 to your computer and use it in GitHub Desktop.
uploadImage method
+ (BOOL)uploadImage:(UIImage *)image withName:(NSString *)fileName toURL:(NSURL *)url {
NSData *imageData = UIImageJPEGRepresentation(image, 100);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"0x0hHai1CanHazB0undar135";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding: NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"imageToAttach\"; filename=\"%@\"\r\n",fileName]dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@",returnString);
return YES;
}
@davejlong
Copy link

That is one of the scariest snippets of code I've ever seen... and I've written a Hello World app in Brainf*ck. There's so few spaces.

@shiftregister-vg
Copy link
Author

Bet you never thought you'd have to craft your own http requests eh? Definitely something I've always taken for granted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment