How to send byte array in c# ApiController
I would base64 encode the byte arrays and change your model properties from byte[]
to string
. That should allow you to post these strings to your controller where you can convert them back to byte arrays.
To encode as base64 string use string byteString = Convert.ToBase64String(byteArray)
and convert back using byte[] byteArray = Convert.FromBase64String(byteString)
.
Your updated code might look something like:
public class TestData{ public string PreviewImage { get; set; } public string FileAsByteArray { get; set; }}[Route("function-route")][HttpPost]public HttpResponseMessage Testfunction(TestData t_testData){ // convert base64 string to byte[] byte[] preview = Convert.FromBase64String(t_testData.PreviewImage); byte[] file = Convert.FromBase64String(t_testData.FileAsByteArray); ... HttpResponseMessage httpResponseMessage = Request.CreateResponse(HttpStatusCode.OK); return httpResponseMessage;}
To test using postman you can use javascript functions atob()
and btoa()
to convert a byte array to a base 64 encoded string and vice versa.