Posting byte array to the server

     string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
     byte[] boundaryBytes = System.Text.Encoding.ASCII.GetBytes("--" + boundary + "\r\n");

     HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(MyUrl);

     webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
     webRequest.Method = "POST";

     using (Stream requestStream = webRequest.GetRequestStream())
              // write boundary bytes
              requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);

              // write header bytes.
              string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
              string header = string.Format(headerTemplate, "MyName", "MyFileName", "content type");
              byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
              requestStream.Write(headerbytes, 0, headerbytes.Length);

              using (MemoryStream memoryStream = new MemoryStream(imageBytes))
                      byte[] buffer = new byte[4096];
                      int bytesRead = 0;
                      while ((bytesRead = memoryStream.Read(buffer, 0, buffer.Length)) != 0)
                              requestStream.Write(buffer, 0, bytesRead);

               // write trailing boundary bytes.
               byte[] trailerBytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
               requestStream.Write(trailerBytes, 0, trailerBytes.Length);


       using (HttpWebResponse wr = (HttpWebResponse)webRequest.GetResponse())
               using (Stream response = wr.GetResponseStream())
                    // handle response stream.

This reads a MemoryStream and writes the data to the requestStream, with a 4096 byte buffer. This should be wrapped in a try-catch so it can trap exceptions and handle them.

Use WebRequest for Posting data as :

WebRequest request = WebRequest.Create ("MyURL"); 
request.Method = "POST"; 
string postData = "This is a test that posts this string to a Web server.";
request.ContentType = "image/jpeg"; 
request.ContentLength = byteArray.Length; 

//Here is the Business end of the code... 
Stream dataStream = request.GetRequestStream (); 
dataStream.Write (byteArray, 0, byteArray.Length); 
dataStream.Close (); 
//and here is the response. 
WebResponse response = request.GetResponse (); 

//Writing response from server

dataStream = response.GetResponseStream (); 
StreamReader reader = new StreamReader (dataStream); 
string responseFromServer = reader.ReadToEnd (); 
Console.WriteLine (responseFromServer); 
reader.Close (); 
dataStream.Close (); 
response.Close (); 

You need use memStream as a reference to the request stream e.g.

Stream memStream = request.GetRequestStream();

Then when you write to it you are writing to the request.


  1. Bruno

    • 2016/5/1

    WebAPI v2.1 and beyond supports BSON (Binary JSON) out of the box, and even has a MediaTypeFormatter included for it.

  2. Girard

    • 2016/7/12

    I am using POST to sent a byte array and a string to the server but is not sucessfull, am I doing the right thing? memStream.Write(image, 0, signature.Length);, image is a byte array. Code: Uri

  3. Khari

    • 2019/8/11

    if HttpClient is used to simply post a ByteArrayContent, the server side should be able to pickup the ByteArrayContent from the HttpRequest body 

  4. Esposito

    • 2016/5/17

    i am new with window phone application, i want to post an image to the server but image data should be in byte array not in base 64 string and i have to post it in multipart, server side code has been written in php. Please help to resolve this problem. Thanks

  5. Ashton

    • 2015/12/6

    feature when send post request to your web server use curl in command line,. Web. Mobile. Cloud NET API browser. System. GetByteArrayAsync( 

  6. Winston

    • 2016/10/8

    Sample Code /** * After the word document is generated in memory we can upload it to the server. * * @param fileContents The byte array we're wanting to POST * @param filename The name of the file

  7. Jeremias

    • 2018/8/27

    responseText if (arrayBuffer) { var byteArray = new a text file on-the-fly and uses the POST method to send the "file" to the server.

  8. Mason

    • 2019/1/22

    request.Method = "POST" ' Create POST data and convert it to a byte array. Dim postData As String = "This is a test that posts this string to a Web server." Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData) ' Set the ContentType property of the WebRequest.

  9. Kaison

    • 2017/12/22

    I would like to post the byte[] data to the server using Web API. The controller will write the data to disk. The catch is that the client 

  10. Damon

    • 2018/5/17

    This might help c# - Post byte array to Web API server using HttpClient - Stack Overflow Member 8921416 23-Jan-19 3:20am I've seen that example and didn't see useful code snippets on server side.

  11. Caiden

    • 2020/8/20

    "POST"; // Create POST data and convert it to a byte array. string postData = "This is a test that posts this string to a Web server.

  12. Tyler

    • 2017/7/8

    The send method of the XMLHttpRequest has been extended to enable easy transmission of binary data by accepting an ArrayBuffer, Blob, or File object. The following example creates a text file on-the-fly and uses the POST method to send the "file" to the server. This example uses plain text, but you can imagine the data being a binary file instead.

  13. Abraham

    • 2021/1/19

    The following code example converts a string entered from the console to a Byte array and posts the array to the specified server using UploadData.

  14. Davion

    • 2020/12/7

    Yes, it does take byte array and I tried that as well. But however, when I tried that what I noticed is that, on the server, I print the request headers. In that one of them is the content-length and I see the content length to be of 4187752 instead of 37254 which is the size after GZip compression.

  15. Jayson

    • 2018/2/26

    I saw this answer in C#: Post byte array to Web API server using HttpClient. I need something like that in Java. Encoding the byte array in 

  16. Luciano

    • 2018/3/15

    Note : My Server is on java platform, so i want to convert the content of byte in byte[]. I am new to iOS development. How can i achieve this ? So far, I have 

  17. Ahmir

    • 2020/7/18

    We know that WWW has a contructor with a byte[] parameter as the post data, and it's quite handy to deal with encoding senstive servers.

Comments are closed.

Recent Posts