0

我有一个 WCF 网络服务来接收文件,但是当我进行 post 调用时,当我尝试发送文件时,总是返回我不受支持的媒体类型 multipart-form-data,如果我不发送文件并将 contentype 设置为 xml,则服务给我一个 OK并创建文件 pruebffa.jpg 很好,这是我的 web.config:

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="streamBinding" transferMode="Streamed" messageEncoding="Mtom" maxReceivedMessageSize="615424521" openTimeout="00:10:00" sendTimeout="00:10:00" receiveTimeout="00:10:00"/>
        </basicHttpBinding>
    </bindings>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
    <services>
        <service name="WcfService3.Service1" behaviorConfiguration="WcfService3.Service1Behavior">
            <endpoint address="" binding="basicHttpBinding" bindingConfiguration="streamBinding" contract="WcfService3.IService1">
                <identity>
                    <dns value="localhost"/>
                </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="WcfService3.Service1Behavior">
                <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
                <serviceMetadata httpGetEnabled="true"/>
                <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
                <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>

代码:

[ServiceContract]
public interface IService1
{
    [OperationContract]
    [WebGet(BodyStyle=WebMessageBodyStyle.Bare, UriTemplate="")] 
    void UploadImage(Stream image);
}

public class Service1 : IService1
{
    public void UploadImage(Stream image)
    {
        var buf = new byte[1024];
        var path = Path.Combine(@"C:\", "pruebffa.jpg");
        int len = 0;
        using (var fs = File.Create(path))
        {
            while ((len = image.Read(buf, 0, buf.Length)) > 0)
            {
                fs.Write(buf, 0, len);
            }
        }
    }
}

如何启用以接受多部分/表单数据?

4

1 回答 1

0

由于这是一个 POST 操作,您应该使用WebInvoke而不是WebGet.

[OperationContract]     
[WebInvoke(..)]      
void UploadImage(Stream image); 
于 2012-08-29T12:29:26.633 回答