title: Upload file from Html form (multipart/form-data) to WCF REST service as a stream without streaming the whole form's inputs?
body: <p>I'm currently having an issue while uploading a file from Html to my rest service (WCF REST). While uploading a file, I'd like to send information like Title and Description along with the file's Contents.</p>
  
  <p>So, I've created a test form like this:</p>
  
  <pre><code>&lt;form id="testForm" action="http://localhost.:1576/NotepadService.svc/Note/91f6413c-4d72-42ca-a0f3-38df15759fc9/Attachment" method="POST" enctype="multipart/form-data"&gt;
          &lt;table&gt;
              &lt;tr&gt;&lt;td&gt;Title:&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="Title"&gt;&lt;/td&gt;&lt;/tr&gt;
              &lt;tr&gt;&lt;td&gt;Description:&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="Description"&gt;&lt;/td&gt;&lt;/tr&gt;
              &lt;tr&gt;&lt;td&gt;Filename:&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="Filename"&gt;&lt;/td&gt;&lt;/tr&gt;
              &lt;tr&gt;&lt;td&gt;File:&lt;/td&gt;&lt;td&gt;&lt;input type="file" name="Contents"&gt;&lt;/td&gt;&lt;/tr&gt;
              &lt;tr&gt;&lt;td/&gt;&lt;td&gt;&lt;input type="submit" value="Send"&gt;&lt;/td&gt;&lt;/tr&gt;
          &lt;/table&gt;
      &lt;/form&gt;
  </code></pre>
  
  <p>Server-side, I'd like to translate it to this method:</p>
  
  <pre><code>[OperationContract]
          [WebInvoke(
              BodyStyle = WebMessageBodyStyle.Bare,
              Method = "POST",
              UriTemplate = "/Note/{noteId}/Attachment")]
          [Description("Add an attachment to a Note.")]
          void AddAttachmentToNote(string noteId, AttachmentRequestDto attachmentRequestDto);
  </code></pre>
  
  <p>With AttachmentRequestDto defined as</p>
  
  <pre><code>[DataContract]
      public class AttachmentRequestDto
      {
           [DataMember]
           public string Title { get; set; }
           [DataMember]
           public string Description { get; set; }
           [DataMember]
           public string Filename { get; set; }
           [DataMember]
           public Stream Contents { get; set; }
      }
  </code></pre>
  
  <p>So, long story short, I'd like to get the Title and Description as string values, while getting the contents of the file as a stream. This doesn't seem to work, as the html form will put all the contents of the form (so also Title and Description) into a stream, along with the contents of the file. Therefor, defining my REST method as</p>
  
  <pre><code>[OperationContract]
          [WebInvoke(
              BodyStyle = WebMessageBodyStyle.Bare,
              Method = "POST",
              UriTemplate = "/Note/{noteId}/Attachment")]
          [Description("Add an attachment to a Note.")]
          void AddAttachmentToNote(string noteId, Stream formContents);
  </code></pre>
  
  <p>works, but then I need to parse the stream to get all of my data (which is not a nice approach, compared to what I actually want to do).</p>
  
  <p>Maybe I need to define 2 different service methods, one accepting only the file, and the other accepting the file's details? That would, however, mean that my business rules (Title required + filecontents required) should be validated differently (as REST is stateless).</p>
  
  <p>Something that might be worth mentioning: I need to save the file's contents in the database, not on the file system.</p>
  
  <p>Does anyone have some input on this one? I'm kind of stuck on it...</p>
  
  <p>Thanks!</p>