Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
466 views
in Technique[技术] by (71.8m points)

html - WCF service to accept a post encoded multipart/form-data

Does anyone know, or better yet have an example, of a WCF service that will accept a form post encoded multipart/form-data ie. a file upload from a web page?

I have come up empty on google.

Ta, Ant

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So, here goes...

Create your service contract which an operation which accepts a stream for its only parameter, decorate with WebInvoke as below

[ServiceContract]
public interface IService1 {

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

}

Create the class...

    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
    }

And the config, to accept streamed data, and the maximum size

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

Also in the System.Web increase the amount of data allowed in System.Web

<system.web>
        <otherStuff>...</otherStuff>
        <httpRuntime maxRequestLength="2000000"/>
</system.web>

This is just the basics, but allows for the addition of a Progress method to show an ajax progress bar and you may want to add some security.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...