title: C# multipart/form-data Unexpected end of MIME multipart stream
body: <p>i have a problem during recive of multipart/form-data POST request, but i can't fuond a problem inside my Body. After send request, server response me With this error </p>
  
  <blockquote>
    <p>Unexpected end of MIME multipart stream. MIME multipart message is not
    complete.</p>
  </blockquote>
  
  <p>Follow the Body of the request.</p>
  
  <pre><code>-------BoundaryNg6f698dj968io34abg32rfa3
  
  Content-Disposition: form-data; name="json"; filename="json"; Content-Type:application/json
  
  {"key":"value","key":"value","key":"value","key":"value","key":"value"}
  
  -------BoundaryNg6f698dj968io34abg32rfa3
  
  Content-Disposition: form-data; name="style"; filename="style.css"; Content-Type:text/css
  
  body {
      width: 100%;
      margin: 0;
      padding: 0;
      [etc etc]}
  
  
  -------BoundaryNg6f698dj968io34abg32rfa3
  
  Content-Disposition: form-data; name="play_circle"; filename="play_circle.png"; Content-Type:image/png
  
  
  [Image]
  
  -------BoundaryNg6f698dj968io34abg32rfa3
  </code></pre>
  
  <p>in my opinion all the body seems to be correct but i'm tired, maybe some one can help me with my problem?</p>
  
  <p>@edit</p>
  
  <p>Follow you can see how i build the multypart Body:</p>
  
  <p>this part build the Css and Json Part.</p>
  
  <pre><code>public byte[] bodyBuilder()
          {
              byte[] bodyAtt = new byte[1];
              StringBuilder sb = new StringBuilder();
              foreach (PostData p in postData)
              {
                  sb.Append(string.Format("\r\n{0}", boundary));
                  sb.AppendLine("\r\n");
                  sb.Append(string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"; Content-Type:{2}", p.Name,p.FileName, p.Type));
                  sb.AppendLine("\r\n");
                  sb.Append(string.Format("{0}\r\n", p.Value));
              }
  
              bodyAtt = encoding.GetBytes(sb.ToString());
              if (content_Type.Contains("multipart/form-data"))
              {
                  bodyAtt = Combine(bodyAtt, setAttachPostData());
              }
  
              var str = Encoding.Default.GetString(bodyAtt);
  
              string pathFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Log.txt");
              File.WriteAllText(pathFile, str);
  
              return bodyAtt;
          }
  </code></pre>
  
  <p>This is for the image.</p>
  
  <pre><code>public byte[] setAttachPostData()
          {
  
              byte[] bodyAtt = new byte[1];
  
              foreach (PostDataParam p in postDataParams)
              {
                  StringBuilder sb = new StringBuilder();
                  sb.AppendLine("\r\n");
                  sb.Append(string.Format("{0}", boundary));
                  sb.AppendLine("\r\n");
                  sb.AppendLine(string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"; Content-Type:{2}", p.Name, p.FileName, p.Type));
                  sb.AppendLine("\r\n");
                  bodyAtt = Combine(bodyAtt, encoding.GetBytes(sb.ToString()));                     bodyAtt = Combine(bodyAtt, p.data);
              }
              bodyAtt = Combine(bodyAtt, encoding.GetBytes(string.Format("\r\n\r\n{0}\r\n", boundary)));
  
              return bodyAtt;
          }
  </code></pre>
  
  <p>and the combine method is simply :</p>
  
  <pre><code> public static byte[] Combine(byte[] first, byte[] second)
          {
              byte[] ret = new byte[first.Length + second.Length];
              Buffer.BlockCopy(first, 0, ret, 0, first.Length);
              Buffer.BlockCopy(second, 0, ret, first.Length, second.Length);
              return ret;
          }
  </code></pre>