Upload file to server from iOS Device

05 Oct


NSString *urlString = @”your server url”;


// setting up the request object now

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];

[request setURL:[NSURL URLWithString:urlString]];

[request setHTTPMethod:@”POST”];


NSString *boundary = [NSString stringWithString:@”—————————14737809831466499882746641449″];

NSString *contentType = [NSString stringWithFormat:@”multipart/form-data; boundary=%@”,boundary];

[request addValue:contentType forHTTPHeaderField: @”Content-Type”];



now lets create the body of the post


NSMutableData *body = [NSMutableData data];

[body appendData:[[NSString stringWithFormat:@”\r\n–%@\r\n”,boundary] dataUsingEncoding:NSUTF8StringEncoding]];

NSDate *date = [NSDate date];

NSDateFormatter *format = [[NSDateFormatter alloc] init];

[format setDateFormat:@”MMddyyHHmm”];

NSString *dateString = [format stringFromDate:date];


NSString *strForData = [NSString stringWithFormat:@”Content-Disposition: form-data; name=\”file\”; filename=\”%@.MOV\”\r\n”,dateString];

[body appendData:[[NSString stringWithString:strForData] dataUsingEncoding:NSUTF8StringEncoding]];

//[body appendData:[[NSString stringWithString:@”Content-Disposition: form-data; name=\”file\”; filename=\”natural.MOV\”\r\n”] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithString:@”Content-Type: application/octet-stream\r\n\r\n”] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[NSData dataWithData:vData]];

[body appendData:[[NSString stringWithFormat:@”\r\n–%@–\r\n”,boundary] dataUsingEncoding:NSUTF8StringEncoding]];

// setting the body of the post to the reqeust

[request setHTTPBody:body];

NSURLConnection * connection =

[[NSURLConnection alloc] initWithRequest:request delegate:self];




//connection delegate methods to handle response


-(void)connection:(NSURLConnection *)connection // IN

didReceiveResponse:(NSURLResponse *)response     // IN


NSLog(@”%s: self:0x%p\n”, __func__, self);




– (void)connection:(NSURLConnection *)connection // IN

didReceiveData:(NSData *)data                // IN


NSString *reply = [[[NSString alloc] initWithData:data





Leave a comment

Posted by on October 5, 2012 in Uncategorized


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: