title: jQuery Ajax File Upload to ASP.NET web service with JSON response
body: <p>I am trying to upload a file using jQuery Ajax to a c# Web Service (.asmx). The file is then processed by the Web Service and the result of the operation is returned asynchronously to the calling JavaScript.</p>
  
  <p>The file upload works. However, it requires that I omit the option <code>contentType: 'application/json; charset=utf-8'</code> when calling the <code>$.ajax()</code> function. And this causes the result not to be serialized as XML rather than the expected JSON. And this, in turn, causes jQuery to call the <code>error</code> handler instead of the <code>success</code> handler.</p>
  
  <p>This is my client-side code:</p>
  
  <pre><code>$.ajax({
      url: global.ajaxServiceUrl + '/StartStructureSynchronisation',
      type: 'POST',
      dataType: 'json',
      //Ajax events
      success: function (msg) {
        // this handler is never called
      error: function () {
        // this handler is called even when the call returns HTTP 200 OK
      },
      data: data, // this is a valid FormData() object
      //Options to tell jQuery not to process data or worry about content-type.
      cache: false,
      contentType: false,
      processData: false
  });
  </code></pre>
  
  <p>And this is my server-side code:</p>
  
  <pre><code>[WebMethod(EnableSession = true)]
  public string StartStructureSynchronisation()
  {
      return this.Execute(delegate
      {
          if (HttpContext.Current.Request.Files.Count == 0)
          {
              Global.StructureSyncResult = new SyncResult() { Result = false, Log = new List&lt;string&gt;() { "No file uploaded." } };
          }
          else if (!new List&lt;string&gt;() { ".xls", ".xlsx" }.Contains(Path.GetExtension(HttpContext.Current.Request.Files[0].FileName).ToLower()))
          {
              Global.StructureSyncResult = new SyncResult() { Result = false, Log = new List&lt;string&gt;() { String.Format("({0}) is not a valid Excel file.", HttpContext.Current.Request.Files[0].FileName) } };
          }
          else
          {
              Global.StructureSyncResult = new Synchronization().SyncStructure(HttpContext.Current.Request.Files[0].InputStream, ref Global.DocSyncCurrent, ref Global.DocSyncMax);
          }
  
          return Global.Serializer.Serialize(Global.StructureSyncResult);
      });
  }
  </code></pre>
  
  <p>So, basically, what I am looking for is one of two things:</p>
  
  <ul>
  <li>Either upload the file using <code>contentType: 'application/json; charset=utf-8'</code>. That way, the response will be serialized as JSON. I could even access the file as a parameter of my WebMethod instead of using <code>HttpContext.Current.Request.Files</code>.</li>
  <li>Or find a way to force the WebService to always serialize the returned value as JSON, regardless of what the client says.</li>
  </ul>
  
  <p>Any ideas?</p>
  
  <p>Thanks a lot in advance and kind regards,</p>
  
  <p>Chris.</p>