title: Uploading a file to WCF service using HTML5 and/or jQuery
body: <p>I am trying to upload a file to my WCF service using only HTML5 and JavaScript/jQuery. The WCF service is self hosted from a Windows service instead of IIS, so I don't think I can use php or aspx upload handlers. The files are binary and not text.</p>
  
  <p>Ideally, I would like to end up with a .net stream object where I can write the file locally (server side). At this point I'm just looking for very very basic file uploading.</p>
  
  <p>I can do JSON communication currently, but cant make the leap to binary files. Currently I'm this far and stuck:</p>
  
  <p><strong>Service interface:</strong></p>
  
  <pre><code>[OperationContract]
  [WebInvoke(Method = "POST", UriTemplate = "SendFile")]
  string SendFile(Stream stream);
  </code></pre>
  
  <p><strong>Service Implementation:</strong> </p>
  
  <pre><code>public string SendFile(Stream stream)
          {
              // this works for strings, but i want a FileStream
              StreamReader reader = new StreamReader(stream);
              string data = reader.ReadToEnd();
  
              return data;
          }
  </code></pre>
  
  <p><strong>HTML:</strong></p>
  
  <pre><code> &lt;input type="file" id="fileInput" onchange='sendFile();' /&gt;
  </code></pre>
  
  <p><strong>JavaScript / jQuery:</strong></p>
  
  <pre><code> function sendFile() {
          submitButton.disabled = true; ;
          setInnerText("sending file...");
          var file = document.getElementById('fileInput').files[0];
          var url = "http://10.0.1.9:8732/RainService/SendFile";
          $.ajax({
              url: url,
  
              type: "POST",
              data: file,
          });
      }
  </code></pre>
  
  <p>In the Chrome console I get an "Illegal invocation" error. I could use <code>data: file.name</code> and that would work fine however. I need to send a stream of the file contents so i can rebuild it on the server side.</p>
  
  <p>Any ideas?</p>