Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

html

http

wcf

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

like image 503
Anthony Johnston Avatar asked Aug 30 '09 19:08

Anthony Johnston


People also ask

How is multipart form data encoded?

Multipart/form-data is one of the most used enctype/content type. In multipart, each of the field to be sent has its content type, file name and data separated by boundary from other field. No encoding of the data is necessary, because of the unique boundary. The binary data is sent as it is.

Is multipart form data restful?

Multipart/Form-Data is a popular format for REST APIs, since it can represent each key-value pair as a “part” with its own content type and disposition. Each part is separated by a specific boundary string, and we don't explicitly need Percent Encoding for their values.


2 Answers

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.

like image 92
Anthony Johnston Avatar answered Sep 20 '22 17:09

Anthony Johnston


I don't exactly know what you're trying to accomplish here, but there's no built-in support in "classic" SOAP-based WCF to capture and handle form post data. You'll have to do that yourself.

On the other hand, if you're talking about REST-based WCF with the webHttpBinding, you could certainly have a service methods that is decorated with the [WebInvoke()] attribute which would be called with a HTTP POST method.

    [WebInvoke(Method="POST", UriTemplate="....")]     public string PostHandler(int value) 

The URI template would define the URI to use where the HTTP POST should go. You'd have to hook that up to your ASP.NET form (or whatever you're using to actually do the post).

For a great introduction to REST style WCF, check out Aaron Skonnard's screen cast series on the WCF REST Starter Kit and how to use it.

Marc

like image 43
marc_s Avatar answered Sep 21 '22 17:09

marc_s