0

I'm trying to upload a single file (not image) using jQuery Ajax to a WCF web service (svc). I finally got the web.config right for transport, and my SVC is receiving something, but I'm at a loss as to how to get and process the incoming form content...

<input id="ipt_file" type="file" />
<a href='#' onclick="UploadFile();" data-role='button'>Upload</a>

var UploadFile = function () {
    var file_object = $('#ipt_file')[0].files[0];
    var form_data = new FormData();
    form_data.append('file', file_object);

    var xhr_upload = $.ajax({
        type: "POST",
        headers: { "Cache-Control":"no-cache", "X-Session":"12345678" },
        url: "../Upload.svc/UploadJobFile",
        data: form_data,
        processData: false,
        contentType: false,
        dataType: "json",
        success: function (msg) {
            if (typeof (msg) === "object") {
                var _upload = $.parseJSON(msg.d);
                alert(_upload.status + ': ' + _upload.msg);
            };
        }
    });
};

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Upload
{
    [OperationContract(Name="UploadJobFile")]
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedResponse, ResponseFormat = WebMessageFormat.Json)]
    [return: MessageParameter(Name = "d")]
    public string UploadJobFile() 
    {
        string response = string.Empty;
        HttpRequest request = HttpContext.Current.Request;
        string session_id = request.Headers["X-Session"];
        //
        // ??? need the request file or body
        //
        if (request.ContentLength > 0)
        {
            response = "OK, content length= " + request.ContentLength.ToString(); // works, returns ~ file size
        }
        else
        {
            response = "fail";
        }
        return response;
    }
}

Unfortunately, request.Form["file"], request.Form[0], and request.Params don't seem to work despite the many posts I've read that claim otherwise. If I can't access the request body, I can't create a byte array, stream, or declare it a HttpPostedFile... (actually all I will be doing is setting up a new request to pass it and some additional params to a data service).

What am I missing?

brnwdrng
  • 521
  • 2
  • 8
  • 18

0 Answers0