title: Send Image using POST
body: <p>I have a WPF application and a ASP.NET MVC site. The WPF application uses the Kinect to capture images and these are saved as files. What I want to do is send the files from the WPF application to the ASP.NET MVC site.</p>
  
  <p>I have tried the following which gets the bytes from the image file and converts it to a string using Base64 and then on the other side try to convert the string back to bytes and then back to a file. The whole process works except the files at the end are corrupt and won't load.</p>
  
  <p>Also is the correct way of sending files or would I be better off trying to use Sockets?</p>
  
  <p><strong>WPF Application</strong></p>
  
  <pre><code>var imageUrl = "http://127.0.0.1:18710/Home/Index";
  
  //byte[] imageBytes = set.getImageBytes();
  byte[] imb = System.Text.Encoding.UTF8.GetBytes("imagename=" + ImageName + ".png&amp;image=" + Convert.ToBase64String(File.ReadAllBytes(ImageName + ".png")));
  
  var imageReq = (HttpWebRequest)WebRequest.Create(imageUrl);
  
  imageReq.Method = "POST";
  imageReq.ContentType = "application/x-www-form-urlencoded";
  imageReq.ContentLength = imb.Length;
  
  
  using (Stream os = imageReq.GetRequestStream())
  {
      os.Write(imb, 0, imb.Length);
  }
  </code></pre>
  
  <p><strong>ASP.NET MVC Site</strong></p>
  
  <pre><code>if (image != null &amp;&amp; imagename != null)
  {
      System.IO.File.WriteAllBytes(@"c:\" + imagename, Convert.FromBase64String(image));
  }
  </code></pre>