title: Post file from one MVC3 action to another site
body: <p>This might sound crazy, but I have an MVC3 action (server-side code) that needs to post a file to another web site. I have control over both sites, but the second site never receives the posted file data. I use WebRequest to successfully request a file from the remote site, so I figured I could post the file data using this approach:</p>
  
  <pre><code>private WebRequest _SetupCopyToRequest(string source, string expectedDestination)
  {
      var request = HttpWebRequest.Create(_Settings.CopyToServerUrl);
      var info = new FileInfo(source);
      request.Method = "POST";
      request.ContentLength = info.Length;
      request.ContentType = "multipart/form-data";
      var dispositionValue = String.Format("form-data; name=\"file\"; filename=\"{0}\"", expectedDestination);
      request.Headers.Set("Content-Disposition", dispositionValue);
      using (var destStream = request.GetRequestStream())
      {
          using (var sourceStream = info.OpenRead())
          {
              var length = (int) info.Length;
              var buffer = new byte[info.Length];
              sourceStream.Read(buffer, 0, length);
              destStream.Write(buffer, 0, length);
          }
      }
      return request;
  }
  </code></pre>
  
  <p>My problem is that the action on the receiving site receives the request as soon I call <code>request.GetRequestStream()</code> on the first site, and Request.Files is empty on the second site. Here is the code for the receiving action on the second site:</p>
  
  <pre><code>[HttpPost]                                                           
  public ActionResult CopyToServer()
  {
      if (Request.Files.Count == 0 || Request.Files[0].ContentLength == 0)
          return new ContentResult();
  
      var file = Request.Files[0];
      var fileName = Path.GetFileName(file.FileName);
      var directory = Path.GetDirectoryName(file.FileName);
      var uniqueFileName = CeoPath.GetUniqueFileName(directory, fileName);
      var path = Path.Combine(directory, uniqueFileName);
      file.SaveAs(path);
      return new ContentResult {Content = path};
  }
  </code></pre>
  
  <p>So how do I get my file data from the first site's server posted to the second site's server?</p>