4

I am trying to upload a image which i am clicking with the help of the camera. I am trying the following code to upload the image to the remote server.

-(void)searchAction:(UIImage*)theImage
{
    UIDevice *dev = [UIDevice currentDevice];
    NSString *uniqueId = dev.uniqueIdentifier;

    NSData * imageData = UIImagePNGRepresentation(theImage);

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

    NSString *urlString = [@"http://www.amolconsultants.com/im.jsp?" stringByAppendingString:@"imagedata=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 == nil) 
    {
        NSLog(@"Failed to create the connection");
    }
}

But nothing is getting posted. Nothing comes in the console window also. I am calling this method in the action sheet. When the user clicks on the 1st button of the action sheet this method is called to post the image.

Can anyone help me with this...

Any code will be very helpful...

Thanx in advance...

Atulkumar V. Jain
  • 5,102
  • 9
  • 44
  • 61

1 Answers1

3

You can use the libraries from a third party called asi-http-request. It simplyfies for you most of the hard works. In your case, you just do as the following:

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request appendPostData:imageData];
[request setRequestMethod:@"PUT"];
Hoang Pham
  • 6,899
  • 11
  • 57
  • 70
  • but i am trying to upload a image from the iphone. the image is not in my mac it will be clicked by the user using the iphone camera and i want to post this image to the server. Can you help me with this... – Atulkumar V. Jain Mar 11 '10 at 17:00
  • I'am talking about iphone here, not mac. You can save the image into the Documents folder and then do just the same. – Hoang Pham Mar 11 '10 at 17:09
  • Use the following function of the NSData: [urlData writeToFile:filePath atomically:NO]; – Hoang Pham Mar 11 '10 at 17:20
  • or else, you don't need to save to documents folder, here you have already the nsdata, use can use the other function of the ASI library to post NSData directly. – Hoang Pham Mar 11 '10 at 17:22
  • Its giving me errors stating ASIHTTPRequest undeclared first use in this method. Do I have to import some header file or add some frameworks... – Atulkumar V. Jain Mar 11 '10 at 17:35
  • ohh, you have to follow the setup instructions on the site I gave you in order to call that function. – Hoang Pham Mar 11 '10 at 17:44