【发布时间】:2011-07-12 17:16:01
【问题描述】:
我正在开发一个 WCF Web 服务,该服务需要能够上传文件等。
目前我添加“平面图”项目的方法如下:
[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,如:
<?xml version="1.0" encoding="us-ascii" ?>
<CreateFloorplanRequest>
<Token></Token>
<Floorplan></Floorplan>
<Image></Image>
</CreateFloorplanRequest>
并且图像包含一个 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:
<service behaviorConfiguration="someBehavior" name="blah.blahblah">
<endpoint
address="DataEntry"
behaviorConfiguration="web"
binding="webHttpBinding"
bindingConfiguration="basicBinding"
contract="blah.IDataEntry" />
</service>
<bindings>
<webHttpBinding>
<binding name="basicBinding" maxReceivedMessageSize ="50000000"
maxBufferPoolSize="50000000" >
<readerQuotas maxDepth="500000000"
maxArrayLength="500000000" maxBytesPerRead="500000000"
maxNameTableCharCount="500000000" maxStringContentLength="500000000"/>
<security mode="None"/>
</binding>
</webHttpBinding>
</bindings>
【问题讨论】:
标签: c# wcf rest file-upload