Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Multipart POST to upload jpeg sample code for iOS
@interface ImageUploader : NSObject {
NSData *theImage;
@property (retain) NSData *theImage;
- (void) syncUpload:(NSData *) uploadImage;
#import "ImageUploader.h"
#define NOTIFY_AND_LEAVE(X) {[self cleanup:X]; return;}
#define DATA(X) [X dataUsingEncoding:NSUTF8StringEncoding]
// Posting constants
#define IMAGE_CONTENT @"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n"
#define BOUNDARY @"------------0x0x0x0x0x0x0x0x"
@implementation ImageUploader
@synthesize theImage;
- (void) syncUpload:(NSData *) uploadImage
self.theImage = uploadImage;
[self main];
- (void) cleanup: (NSString *) output
self.theImage = nil;
// NSLog(@"******** %@", output);
- (NSData*)generateFormDataFromPostDictionary:(NSDictionary*)dict
id boundary = BOUNDARY;
NSArray* keys = [dict allKeys];
NSMutableData* result = [NSMutableData data];
for (int i = 0; i < [keys count]; i++)
id value = [dict valueForKey: [keys objectAtIndex:i]];
[result appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
if ([value isKindOfClass:[NSData class]])
// handle image data
NSString *formstring = [NSString stringWithFormat:IMAGE_CONTENT, [keys objectAtIndex:i]];
[result appendData: DATA(formstring)];
[result appendData:value];
NSString *formstring = @"\r\n";
[result appendData:DATA(formstring)];
NSString *formstring =[NSString stringWithFormat:@"--%@--\r\n", boundary];
[result appendData:DATA(formstring)];
return result;
// NSOperationQueueによる非同期化を見据えてmainにしています。
- (void) main
if (!self.theImage) {
NOTIFY_AND_LEAVE(@"Please set image before uploading.");
NSString* MultiPartContentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", BOUNDARY];
NSMutableDictionary* post_dict = [[NSMutableDictionary alloc] init];
[post_dict setObject:@"testimage" forKey:@"filename"];
[post_dict setObject:self.theImage forKey:@"data[User][image]"];
NSData *postData = [self generateFormDataFromPostDictionary:post_dict];
[post_dict release];
NSString *baseurl = @"";
NSURL *url = [NSURL URLWithString:baseurl];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
if (!urlRequest) NOTIFY_AND_LEAVE(@"Error creating the URL Request");
[urlRequest setHTTPMethod: @"POST"];
[urlRequest setValue:MultiPartContentType forHTTPHeaderField: @"Content-Type"];
[urlRequest setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData* result = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
//NSLog(@"***** result =%@", result);
if (!result)
[self cleanup:[NSString stringWithFormat:@"upload error: %@", [error localizedDescription]]];
// Return results
NSString *outstring = [[[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding] autorelease];
NSLog(@"***** outstring =%@", outstring);
[self cleanup: outstring];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.