-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

10
  • 4
    That is not a minimal reproducible example. Commented Jul 22, 2020 at 13:24
  • 3
    You probably shouldn't be using dynamic... Not that it would solve this specific problem, but still. Commented Jul 22, 2020 at 13:25
  • 2
    @Rakesh the question still doesn't explain what item["Photo1"] contains. It could be an int[] or long[]. It's definitely not a byte[], which is why you get that exception Commented Jul 22, 2020 at 13:28
  • 4
    JSON is text, not binary data. You need to deserialize or parse that text to get the data. Commented Jul 22, 2020 at 13:29
  • 2
    @Rakesh I strongly suggest you edit the question, including the actual JSON string, and the code that lead to the error. Commented Jul 22, 2020 at 13:30

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.