tags:

views:

55

answers:

2

Hello Guys,

I have a WCF service that my iPhone app is connected to. Now I need to add an upload photo feature to my iPhone app. That said, I needed to have my WCF service to accept Images and save it to my server. Any idea how to achieve this?

I did googling for some possible solutions but neither of them works for me.

Your responses is greatly appreciated.

A: 

I use the ASIHTTPRequest client library for my iPhone apps (http://allseeing-i.com/ASIHTTPRequest/). Uploading a file with this library is trivial. If you can configure your services to accept vanilla posts for file uploads, you're done.

Rafael Vega
What do you mean? I don't quiet understand. Can give me a brief example please?
samer
Can I use byte[] on my WCF method? and use ASIHTTPRequest to send the image file?
samer
When you make a POST with file data with ASIHTTPRequest it does the exact same thing a browser with <form action="whatever" method="POST" enctype="multipart/form-data"> would do. On the server side you can handle the POST as you would normally do.
Rafael Vega
A: 

sample code snippet, no error checking just basic code for posting image

// get image from somewhere...
NSData *imageData = UIImageJPEGRepresentation(image, 90);

NSString *urlString = @"http://yourserver.com/upload_image";

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSMutableData *postBody = [NSMutableData data];

//add image data to POST
[postBody appendData:[NSData dataWithData:imageData]];

[request setHTTPBody: postBody];

// connect to the web
NSData *respData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *respStr = [[NSString alloc] initWithData:respData encoding:NSUTF8StringEncoding];
Aaron Saunders
Thanks for the sample. Greatly appreciated... :-)
samer
Now I need the WCF part.
samer
@samer 1) that is a different question and 2) you said you already had the WCF service? I would suggest you post that question and accept this one.
Aaron Saunders
Yes I did but in my question I stated that I needed my WCF service to accept image from my iphone app. I already have the same code that you posted above(Not exactly the same but will do uploading of image). Sorry I can't accept your answer.
samer
@samer good luck
Aaron Saunders