WCF REST文件上载

我正在开发一个WCF Web服务,需要能够上传文件等。

目前我添加’floorplan’项的方法如下:

[OperationContract] [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "Floorplan?token={token}&floorplan={floorplan}")] string XmlInputFloorplan(string token, string floorplan); 

我需要更改它,以便将图像作为此调用的一部分上载,可以在以下方法中使用:

 public static Guid AddFile(byte[] stream, string type); 

在这种情况下, byte[]是图像的内容。 然后将得到的guid传递给数据层,并最终确定平面图的添加。

所以我需要弄清楚两件事:

1)我应该如何改变XmlInputFloorplan接口方法,以便它还允许图像作为参数?
2)如何在更改后使用服务?

谢谢!

这是我解决它的方式:

 [OperationContract] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "Floorplan")] XmlDocument XmlInputFloorplan(Stream content); 

期待输入XML,如:

       

Image包含一个base 64编码的字符串,表示我通过以下方式转换为byte []的图像文件:

 XmlDocument doc = new XmlDocument(); doc.Load(content); content.Close(); XmlElement body = doc.DocumentElement; byte[] imageBytes = Convert.FromBase64String(body.ChildNodes[2].InnerText); 

为了实现这一点,我必须像这样配置Web.config:

            

你的URI看起来会完全不同 – 就像这样(我不得不做一些猜测)

 [OperationContract] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "Floorplan?type={type}&token={token}&floorplan={floorplan}")] Guid XmlInputFloorplan(string type, string token, string floorplan, Stream image); 

我已经冒昧地将字节数组更改为Stream,如果它很大(但不需要流式传输),您可以选择流式传输图像

要调用它,您可以使用正确的Uri(包括类型,令牌和布局图)创建WebRequest并执行POST。 使内容类型为图像格式(jpeg,png等)的正确类型,并获取将图像复制到其中的请求流。 然后在WebRequest上调用GetResponse来发出HTTP请求

您将无法将字节数组作为GET传递。 在请求字符串中传递那么多数据wouldent work。 你需要做一个http POST