[ACCEPTED]-WCF service to accept a post encoded multipart/form-data-http

Accepted answer
Score: 62

So, here goes...

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

public interface IService1 {

        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 6 accept streamed data, and the maximum size

       <binding name="WebConfiguration" 
       <behavior name="WebBehavior">
         <webHttp />         
       <behavior name="Sandbox.WCFUpload.Web.Service1Behavior">
         <serviceMetadata httpGetEnabled="true" httpGetUrl="" />
         <serviceDebug includeExceptionDetailInFaults="false" />
     <service name="Sandbox.WCFUpload.Web.Service1" behaviorConfiguration="Sandbox.WCFUpload.Web.Service1Behavior">
        contract="Sandbox.WCFUpload.Web.IService1" />

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

        <httpRuntime maxRequestLength="2000000"/>

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

Score: 1

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

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

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

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

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


More Related questions