View Full Version : How can I post image (.png) data into the server?

Sep 2, 2008, 09:48 AM

I have written an iphone application that requires to upload an image (.png) into the server. But, I do not know how to upload this image data into the server. I found a thread post regarding the upload of image data into the server but actually I do not know how it actually works. Please see this code:


UIDevice *dev = UIDevice currentDevice;
NSString *uniqueId = dev.uniqueIdentifier;

UIImage *testImage = UIImage imageNamed:@"image1.png";

NSData * imageData = UIImagePNGRepresentation(testImage);

NSString *postLength = [NSString stringWithFormat:@"%d", imageData length];

NSString *urlString = @"http://example.com/rtl?" stringByAppendingString:@"ve=iPhoneV0&mcid";
urlString = [urlString stringByAppendingString:uniqueId];
urlString = urlString stringByAppendingString:@"&lang=en_US.UTF-8";

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] autorelease;
[request setURL:NSURL URLWithString:urlString];
request setHTTPMethod:@"POST";
request setValue:postLength forHTTPHeaderField:@"Content-Length";
request setHTTPBody:imageData;

NSURLConnection *conn=[NSURLConnection alloc] initWithRequest:request delegate:self;
if (conn)
receivedData = [NSMutableData data] retain;

If this code is working then please tell me how to extract data on the server side so that I can insert image data into a database.

Thanks with warm regards.