iPhone应用程序 – 如何将图像上传到.net C#WCF?

我有一个我的iPhone应用程序连接到的WCF服务。 现在我需要在我的iPhone应用程序中添加上传照片function。 也就是说,我需要让我的WCF服务接受图像并将其保存到我的服务器。 知道怎么做到这一点?

我搜索了一些可能的解决方案,但它们都不适合我。

非常感谢您的回复。

示例代码段,没有错误检查只是发布图像的基本代码

// 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]; 

我为我的iPhone应用程序使用ASIHTTPRequest客户端库(http://allseeing-i.com/ASIHTTPRequest/)。 使用此库上传文件非常简单。 如果您可以将服务配置为接受文件上传的香草帖,那么您就完成了。

我花了很多时间才得到,但这里是所有人的WCF部分……

接口(Iservice1.cs):

  [OperationContract] [WebInvoke(Method="POST", UriTemplate = @"/FileUpload/")] void FileUpload(Stream stream); 

实现(Service1.svc.cs):

 public string FileUpload(Stream stream) { byte[] buffer = new byte[10000]; stream.Read(buffer, 0, 10000); FileStream f = new FileStream("C:\\temp\\sample.jpg", FileMode.OpenOrCreate); f.Write(buffer, 0, buffer.Length); f.Close(); stream.Close(); return "Recieved the image on server"; }