Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to handle an ajax upload file with SVC?
    text
    copied!<p>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...</p> <pre><code>&lt;input id="ipt_file" type="file" /&gt; &lt;a href='#' onclick="UploadFile();" data-role='button'&gt;Upload&lt;/a&gt; </code></pre> <hr> <pre><code>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); }; } }); }; </code></pre> <hr> <pre><code>[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 &gt; 0) { response = "OK, content length= " + request.ContentLength.ToString(); // works, returns ~ file size } else { response = "fail"; } return response; } } </code></pre> <hr> <p>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).</p> <p>What am I missing?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload