Passing multipart/form-data from ajax to restful wcf with MessageContract parameter RRS feed

  • Question

  • I am able to upload file using ajax to WCF restful service with the parameter as Stream But It does not work if parameter is changed to MessageContract.

    The following code works:

    [WebInvoke(Method = "POST", UriTemplate = "UploadCaseDocuments")]
    void UploadCustomFile(System.IO.Stream stream);

    But the following Fails

    [WebInvoke(Method = "POST", UriTemplate = "UploadCaseDocuments")]
    void UploadCustomFile(UploadStreamMessage stream);

        public class UploadStreamMessage
            public string appRef;
            public System.IO.Stream data;

    Ajax Code:
                         crossDomain: true,
                         cache: false,
                         contentType: false,
                         processData: false,
                         type: this.method,
                         beforeSend: function (request) {
                             if (headerCollection != null) {
                                 for (var k in headerCollection) {
                                     if (headerCollection.hasOwnProperty(k)) {
                                         request.setRequestHeader(k, headerCollection[k]);
                         async: true,
                         data: data,
                         url: this.url,
                         dataType: "json"

    I need to pass File along with additional data in MessageContract in WCF restful service using Ajax. Any Example will be very helpful. Thank you in advance

    Friday, August 22, 2014 6:33 AM


  • Hi Munaga,

    Message contracts describe the structure of SOAP messages sent to and from a service and enable you to inspect and control most of the details in the SOAP header and body.

    I see that you are using the WCF restful service, but the message control does not support in the WCF Restful Service. We can only use it in the SOAP Service.

    For more, please see:
    Streaming Files (for Upload/Download) in WCF (Message Contracts) .


    Monday, August 25, 2014 5:59 AM