title: Read multipart/mixed in C# (.NET 4.0)
body: <p>I get a multipart/mixed (with a JSON object and a file) like this, as response when I call a web service:</p>
  
  <pre><code>"--Boundary_9_15033478_1405613164044\r\n
  
  Content-Type: application/json
  \r\n\r\n{\"docId\":9007492,\"protId\":200,\"protNum\":\"0002084B14\",\"protDate\":\"Wed Jul 16 00:00:00 EEST 2014\",\"categoryId\":1000,\"desc\":\"ѥⷞ ��ƣ䲜��",\"linkNum\":\"ư1\\/00005545\",\"flag1\":\"\",\"flag2\":\"\",\"flag3\":\"\",\"flag4\":\"\",\"stsId\":1,\"stsDesc\":\"WS04: Check Layer I - OK\",\"wsDataList\":[],\"fileName\":\"WsTestToolkitMain.jpg\"}\r\n--Boundary_9_15033478_1405613164044\r\n
  
  Content-Type: application/octet-stream
  \r\n\r\n????\0JFIF\0\0`\0`\0\0??\0C\0\a\a\a\a\a\a\b\t\v\t\b\b\n\b\a\a\n\r\n\n\v\f\f\f
  ...
  ...
  ...
  ??\n?$??\0???????\r\n--Boundary_9_15033478_1405613164044--\r\n"
  </code></pre>
  
  <p>The way I got it is by getting a Stream of the response (of the HttpWebRequest) and then decode it with UTF-8. This gives me the above String. <strong>The question is</strong> how can I get the JSON and (more importantly) save <strong>the file</strong> ?</p>
  
  <p>I tried changing (because they are for multipart/form-data not multipart/mixed) <a href="http://antscode.blogspot.ie/2009/11/parsing-multipart-form-data-in-wcf.html" rel="noreferrer" title="this">this</a> and <a href="https://bitbucket.org/lorenzopolidori/http-form-parser/wiki/Home" rel="noreferrer" title="this">this</a> but I can't get the file, maybe because its Content-Type is <strong>application/octet-stream</strong>  . Here is what doesn't work, file appears as it was damaged/corrupted in windows:</p>
  
  <pre><code>// Read the stream into a byte array
  byte[] data = ToByteArray(stream);//Source
  string content = encoding.GetString(data);
  int contentLength = content.Length;//Length
  byte[] fileData = new byte[contentLength];//Destination
  Buffer.BlockCopy(data, startIndex, fileData, 0, contentLength);
  this.FileContents = fileData;
  File.WriteAllBytes("G:\\" + parser.Filename, parser.FileContents);
  </code></pre>
  
  <p><strong>UPDATE:</strong>
  Followed the answer by Anton Tykhyy (thanks!) but I'm getting an error when </p>
  
  <pre><code>var multipart = await content.ReadAsMultipartAsync () ;
  </code></pre>
  
  <p><strong>Invalid 'HttpContent' instance provided. It does not have a content-type header value. 
  'HttpContent' instances must have a content-type header starting with 'multipart/'.</strong></p>
  
  <p>I tried to add this line before         </p>
  
  <pre><code>content.Headers.Add("Content-Type", "multipart/mixed");
  </code></pre>
  
  <p>but now I'm getting another error (that I don't really understand too)</p>
  
  <p><strong>Invalid 'HttpContent' instance provided. It does not have a 'multipart' content-type header with a 'boundary' parameter.</strong></p>
  
  <p><strong>UPDATE 2:</strong>
  Found it, I just had to do this:</p>
  
  <pre><code>    var content = new StreamContent(httpResponse.GetResponseStream());
      content.Headers.Add("Content-Type", httpResponse.ContentType);
  </code></pre>