How can I post image (.png) data into the server?

Discussion in 'iOS Programming' started by sspbond007, Sep 2, 2008.

  1. sspbond007 macrumors member

    Aug 19, 2008

    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:mad:"image1.png";

    NSData * imageData = UIImagePNGRepresentation(testImage);

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

    NSString *urlString = @"" stringByAppendingString:mad:"ve=iPhoneV0&mcid";
    urlString = [urlString stringByAppendingString:uniqueId];
    urlString = urlString stringByAppendingString:mad:"&lang=en_US.UTF-8";

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] autorelease;
    [request setURL:NSURL URLWithString:urlString];
    request setHTTPMethod:mad:"POST";
    request setValue:postLength forHTTPHeaderField:mad:"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.

Share This Page