使用其他参数WCF将图像上载为流

时间:2014-01-05 18:13:31

标签: c# wcf rest

我正在尝试通过WCF在数据库中保存图像。这是我的代码。

public void saveImage(Stream stream, string size)
    {
        //int intsize = Convert.ToInt32(size);
        byte[] buffer = new byte[10000];
        int bytesRead, totalBytesRead = 0;
        string encodedData = "";

        do
        {
            bytesRead = stream.Read(buffer, 0, buffer.Length);
            encodedData = encodedData + Convert.ToBase64String(buffer,
                                       Base64FormattingOptions.InsertLineBreaks);
            totalBytesRead += bytesRead;
        } while (bytesRead > 0);

这是合同。

    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "SaveImage/{size}")]
    void saveImage(Stream stream, string size);

最后这是我的配置文件的一部分

<system.serviceModel>
<services>
  <service behaviorConfiguration="RestServiceBehavior" name="ABBStreamService.ABBConnectStreamWCF">
    <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" bindingConfiguration="webHttpBinding" contract="ABBStreamService.IABBConnectStreamWCF" />
  </service>
</services>
<bindings>
  <webHttpBinding>
    <binding name="webHttpBinding" transferMode="Streamed" maxReceivedMessageSize="2147483647" maxBufferSize="10485760" closeTimeout="00:01:00" openTimeout="00:01:00"
             receiveTimeout="00:10:00" sendTimeout="00:01:00">
      <readerQuotas maxStringContentLength="2147483647" maxArrayLength="1000000" />
    </binding>
  </webHttpBinding>
</bindings>
<behaviors>
  <serviceBehaviors>
    <behavior name="RestServiceBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="web">
      <webHttp />
    </behavior>
  </endpointBehaviors>
</behaviors>
<protocolMapping>
    <add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>    
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>

当我尝试仅使用Stream作为参数运行服务时,它可以工作。但是当我尝试添加另一个参数时,它失败了。

2 个答案:

答案 0 :(得分:0)

您可以在请求中添加标题,然后在服务中读取该标题。

 //Javascript to set the header
 var xhr = new XMLHttpRequest();
 var image = document.getElementById("yourFileInput").files[0];
 xhr.setRequestHeader('size', image.size);

 //C# in your service method to read the size header
 IncomingWebRequestContext request = WebOperationContext.Current.IncomingRequest;
 var headers = request.Headers;
 string size = headers["size"];

答案 1 :(得分:0)

我知道我迟到了,但这对搜索者很有用。 在正文中发送文件,在请求标头中发送其余参数。

下面的示例代码在Post man中进行了测试,像魅力一样工作。

您的界面

[WebInvoke(
        Method = "POST",
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,
        UriTemplate = "PostImage")]
        void PostImage(Stream stream);

.svc方法:

public void PostImage(Stream stream)  
        {
            // get image from stream and implement your logic

            var headers = OperationContext.Current.IncomingMessageProperties["httpRequest"];
            // other parameters will be accessible here e.g image name etc
            var imgname = ((HttpRequestMessageProperty)headers).Headers["imgname"];


        }

作为参考,请参见this链接