-1

I am trying cast a Json data to byte array and then save it in SQL image field, code is below

public string Post([FromBody] dynamic data)
{
        foreach (var item in data)
        {
               imgPhoto1 = (byte[])item["Photo1"];
        }
 }

But getting error Can not convert Array to byte array

 byte[] imgPhoto1 = (byte[])item["Photo1"];

Values in field item["Photo1"] is look like below

 [255,216,255,224]

any help will be appreciated

Rakesh
  • 2,730
  • 10
  • 39
  • 65

1 Answers1

3

If your parameter data is JToken, then you need convert it to desired type. Try to use:

var obj = item["Photo1"].ToObject<byte[]>();

And will be better explicit declare your parameter data as JToken.

Alex69rus
  • 46
  • 2