title: sending binary file byte array to web api method
body: <p>I have a requirement to post binary file of size 100MB data in the format of either JSON or byte array to Web API 1.1.
  My client application is C# winforms application with x32 bit architecture. Where as I want to perform reading binary file from this client application and send this binary file byte array to Web API.</p>
  
  <p>Current implementation in my winforms application is as below</p>
  
  <pre><code>var sFile = @"C"\binary.zip";
  var mybytearray = File.ReadAllBytes(sFile);
  var webRequest =
                  (HttpWebRequest)WebRequest.Create("http://localhost/filewriter");
  webRequest.ContentType = "text/plain";
  webRequest.Method = WebRequestMethods.Http.Post;
  webRequest.AllowWriteStreamBuffering = true;
  webRequest.Timeout = 100000;
  webRequest.Headers.Add("fileName", Path.GetFileName(sFile));
  webRequest.ContentLength = mybytearray.Length;
  
  using (var dataStream = new StreamWriter(webRequest.GetRequestStream()))
        dataStream.Write(mybytearray);
  
  using (var response = webRequest.GetResponse())
  {
      if(response.StatusCode = HttpStatusCode.Ok;
           return true;
  }
  </code></pre>
  
  <p>below is written at my Web api method</p>
  
  <pre><code>[HttpPost]
  public HttpResponseMessage filewriter(byte[] binaryData)
  {
      using (FileStream binaryFileStream = new FileStream("C:\\myNewFile.zip", FileMode.Create, FileAccess.ReadWrite))
            {
                binaryFileStream.Write(binaryData, 0, binaryData.Length);
            }
  }
  </code></pre>
  
  <p>As you can see, in above code I was not able to send byte array to web api method filewriter. Am I missing something that should work in this case.</p>
  
  <p>Other way as I said I was tried same but instead of byte array with Json one as below</p>
  
  <pre><code>var sFile = @"C"\binary.zip";   
  var mybytearray = File.ReadAllBytes(sFile);
  var mymodel = new model
  {
      fileName = sFile,
      binaryData = mybytearray
  };
  
  var jsonResendObjects = JsonConvert.SerializeObject(mymodel);
  var webRequest = (HttpWebRequest)WebRequest.Create("http://localhost/filewriter");
  webRequest.ContentType = "application/json";
  webRequest.Method = WebRequestMethods.Http.Post;
  webRequest.AllowWriteStreamBuffering = true;
  webRequest.Timeout = 100000;
  webRequest.Headers.Add("fileName", Path.GetFileName(sFile));
  webRequest.ContentLength = jsonResendObjects.Length;
  byte[] responseData = null;
  
  webRequest.AllowWriteStreamBuffering = true;
  using (var dataStream = new StreamWriter(webRequest.GetRequestStream()))
      dataStream.Write(jsonResendObjects);
  </code></pre>
  
  <p>On web api side</p>
  
  <pre><code>[HttpPost]
  public HttpResponseMessage filewriter([FromBody]model mymodel)
  {
      using (FileStream binaryFileStream = new FileStream("C:\\myNewFile.zip", FileMode.Create, FileAccess.ReadWrite))
            {
                binaryFileStream.Write(mymodel.binarydata, 0, binaryDatabinarydat.Length);
            }
  }
  </code></pre>