WCF REST文件上传 [英] WCF REST File Upload

查看:174
本文介绍了WCF REST文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



目前,我添加平面布局项目的方法如下所示:

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $'
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传递给数据层,并添加了平面布局图。

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


$ b $ 1)我应该如何改变 XmlInputFloorplan 接口方法,以便它也允许图像作为参数?

2)如何在改变后使用服务?

谢谢!

这是我如何解决它:

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

期望输入XML如下:

 <?xml version =1.0encoding =us-ascii?> 
< CreateFloorplanRequest>
< Token>< / Token>
< Floorplan>< / Floorplan>
<图片>< /图片>
< / CreateFloorplanRequest>

图像包含一个基本的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 =someBehaviorname =blah.blahblah> 
address =DataEntry
behaviorConfiguration =web
binding =webHttpBinding
bindingConfiguration =basicBinding
contract = blah.IDataEntry/>
< / service>

< bindings>
< webHttpBinding>
< binding name =basicBindingmaxReceivedMessageSize =50000000
maxBufferPoolSize =50000000>
< readerQuotas maxDepth =500000000
maxArrayLength =500000000maxBytesPerRead =500000000
maxNameTableCharCount =500000000maxStringContentLength =500000000/>
< security mode =None/>
< / binding>
< / webHttpBinding>
< / bindings>


解决方案

您的URI看起来完全不同 - 我不得不做一些猜测)
$ b $ pre $ [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请求

I am developing a WCF web service that needs to be able to upload files among other things.

Currently my method for adding a 'floorplan' item looks like:

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

I need to alter it so that an image will be uploaded as a part of this call that can be used in a method like:

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

In this case the byte[] is the content of the image. The resulting guid is then passed on to the data layer and the addition of the floorplan is finalized.

So I need to figure out two things:

1) How should I alter the XmlInputFloorplan interface method so that it also allows for an image as a parameter?
2) How do I consume the service after the alteration?

Thanks!

Here is how I solved it:

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

Expects an input XML like:

<?xml version="1.0" encoding="us-ascii" ?>
<CreateFloorplanRequest>
  <Token></Token>
  <Floorplan></Floorplan>
  <Image></Image>
</CreateFloorplanRequest>

And the Image contains a base 64 encoded string that represents the image file which I convert to byte[] via:

XmlDocument doc = new XmlDocument();
doc.Load(content);
content.Close();

XmlElement body = doc.DocumentElement;
byte[] imageBytes = Convert.FromBase64String(body.ChildNodes[2].InnerText);

In order to allow for this I had to configure the Web.config like so:

<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>

解决方案

Your URI will look completely different - something like this (I'm having to make some guesses)

[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);

I've taken the liberty of changing the byte array to a Stream which gives you the option of streaming the image should it be large (but doesn' t require streaming)

To call this you can create a WebRequest with the correct Uri (including the type, token and floorplan) and perform a POST. Make the content type the right one for the format of the image (jpeg, png, etc) and get the request stream copying the image into it. Then call GetResponse on the WebRequest to make the HTTP request

这篇关于WCF REST文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆