1

I am trying to upload a photo from iphone to a restful wcf. I have tried many times but failed. Here is some code of mine

Code from client

-(IBAction)uploadClick:(id)sender{
NSString *surl = @"http://192.168.5.226:8068/FileService.svc/UploadFile" ;
NSURL *url = [NSURL URLWithString:surl];

ASIFormDataRequest *r = [ASIFormDataRequest requestWithURL:url];
[r setValidatesSecureCertificate:NO];
[r setTimeOutSeconds:30];
[r setRequestMethod:@"POST"]; //default is POST (insert), 
[r setDelegate:self];
[r setDidFailSelector:@selector(requestDidFail:)];
//[r addRequestHeader:@"Content-Type" value:@"application/json"]   this will cause the call to fail.  No content-type header for this call.

NSMutableData *imageData = [NSMutableData dataWithData:UIImageJPEGRepresentation([UIImage imageWithData:self.pickedFileContent], .35)];
[r setPostBody:imageData];
[r setDidFinishSelector:@selector(imageSaveDidFinish:)];
[r startAsynchronous];}

Code from restful wcf:

 public interface IFileService
{
    [OperationContract, WebInvoke(UriTemplate = "UploadFile", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, Method = "POST")]
    void UploadFile( Stream fileContent);
}
public class FileService : IFileService
{
    public void UploadFile(Stream fileContent)
    {
          convert the stream to a file and save it.
    }
}

when I trigger the uploadClick event from iphone, the image data is converted to NSMutableData, but after [r startAsynchronous], nothing happened it restful wcf. I guess maybe the request didn't reach the wcf or the wcf couldn't recognize it. I want to know what's wrong with it or give me another solution. any comments is appreciated.

user418751
  • 1,937
  • 2
  • 18
  • 21
  • I am not sure about wcf. But for uploading images or other data we use multipart request model. http://stackoverflow.com/questions/9477046/how-to-send-image-data-to-server-i-already-have-a-sample-page/9477819#9477819 , http://stackoverflow.com/questions/9479093/how-to-upload-image-data-to-server-with-specific-file-name#comment11996956_9479093 – Ravin Feb 29 '12 at 08:42

1 Answers1

1

Here you are trying to POST an image data to a web-service. I am not sure if this is the solution you are looking for.

This is how i POST an image data to RESTful service.

You will have to wrap the image data in a boundary parameter in the request. Below code explains how to do it.

NSURL *url = [NSURL urlWithString:@"http://www.sample.com/websevice.php"];
NSMutableRequest *uRequest = [[NSMutableRequest alloc]initWithUrl:url];
//STEP1 Creating NSData from UIImage
NSData *postImageData = UIImageJPEGRepresentation(image, .7);
NSMutableData *body = [NSMutableData data];

//STEP2 Creating Boundary
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];

//Setting POST Header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[uRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];

//STEP3 Begin boundary
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

//STEP4 Adding additional parameters 
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n",fileName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

//STEP5 Appending Image Data
[body appendData:[NSData dataWithData:postImageData]];


//STEP6 Closing boundary
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

// setting the body of the post to the reqeust
[uRequest setHTTPBody:body];
// IMPLEMENT NSURLConnectionDelegate Protocol
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:uRequest delegate:self];
Krrish
  • 2,256
  • 18
  • 21