WCF服务接受post编码的multipart/form-data

Ant*_*ton 32 html wcf http

有没有人知道,或者更好的是有一个WCF服务的例子,它将接受表格后期编码multipart/form-data即.从网页上传文件?

我在谷歌上空了.

塔,蚂蚁

Ant*_*ton 61

所以,这里......

创建一个服务合同,其接受一个流作为其唯一参数的操作,使用WebInvoke进行装饰,如下所示

[ServiceContract]
public interface IService1 {

    [OperationContract]
    [WebInvoke(
        Method = "POST",
        BodyStyle = WebMessageBodyStyle.Bare,
        UriTemplate = "/Upload")]
    void Upload(Stream data);

}
Run Code Online (Sandbox Code Playgroud)

创建班级......

    public class Service1 : IService1 {

    public void Upload(Stream data) {

        // Get header info from WebOperationContext.Current.IncomingRequest.Headers
        // open and decode the multipart data, save to the desired place
    }
Run Code Online (Sandbox Code Playgroud)

和配置,接受流数据,以及最大大小

<system.serviceModel>
   <bindings>
     <webHttpBinding>
       <binding name="WebConfiguration" 
                maxBufferSize="65536" 
                maxReceivedMessageSize="2000000000"
                transferMode="Streamed">
       </binding>
     </webHttpBinding>
   </bindings>
   <behaviors>
     <endpointBehaviors>
       <behavior name="WebBehavior">
         <webHttp />         
       </behavior>
     </endpointBehaviors>
     <serviceBehaviors>
       <behavior name="Sandbox.WCFUpload.Web.Service1Behavior">
         <serviceMetadata httpGetEnabled="true" httpGetUrl="" />
         <serviceDebug includeExceptionDetailInFaults="false" />
       </behavior>
     </serviceBehaviors>
   </behaviors>
   <services>     
     <service name="Sandbox.WCFUpload.Web.Service1" behaviorConfiguration="Sandbox.WCFUpload.Web.Service1Behavior">
      <endpoint 
        address=""
        binding="webHttpBinding" 
        behaviorConfiguration="WebBehavior"
        bindingConfiguration="WebConfiguration"
        contract="Sandbox.WCFUpload.Web.IService1" />
    </service>
  </services>
 </system.serviceModel>
Run Code Online (Sandbox Code Playgroud)

同样在System.Web中增加System.Web中允许的数据量

<system.web>
        <otherStuff>...</otherStuff>
        <httpRuntime maxRequestLength="2000000"/>
</system.web>
Run Code Online (Sandbox Code Playgroud)

这只是基础知识,但允许添加Progress方法来显示ajax进度条,您可能希望添加一些安全性.

  • 如何使用Stream删除所有正在发送的垃圾:Content-Disposition:,Content-Type:etc ...我试图让它适用于图像.另外,为什么任何其他参数都不能在合同定义中 (2认同)