1

I need to code a method in a WebAPI application where I need to send an Image, this is what I'm doing:

 public IHttpActionResult PostPicture(string xmit, int contactNum, [FromBody] byte[] picture) { Image img = null; using (var ms = new MemoryStream(picture)) { img = Image.FromStream(ms); } if (img != null) { return Ok(); } else { return BadRequest(); } } 

The problem is that when I'm sending the byte[] into the body in the request the value of the picture parameter is always null. I'm using Postman to test the method and the request body is being send as a x-www-form-urlencoded. Postman Test Could anyone help me to find what I'm doing wrong?

9
  • that looks like base64 encoding. have you tried changing it (the parameter) to a string and then converting to byte array Commented Nov 14, 2017 at 13:20
  • @Nkosi No I didn't try that, I will do and I let you know. Thanks Commented Nov 14, 2017 at 13:21
  • 1
    Another option would be to remove the parameter and read it directly from the request imageStream = await Request.Content.ReadAsStreamAsync() Commented Nov 14, 2017 at 13:23
  • @Nkosi I change the code to get string parameters but it keeps getting null I really don't know why Commented Nov 14, 2017 at 13:27
  • I can not understand why someone could vote down a programming question without let people know why is the reason for, I don't see any problem in this question at all, who did please explain why? So the next time we don't make the same mistake Commented Nov 14, 2017 at 13:31

1 Answer 1

1

Thanks to @Nkosi, this solved the problem: Another option would be to remove the parameter and read it directly from the request imageStream = await Request.Content.ReadAsStreamAsync() actually it didn't work before because the way I was doing the request in Postman. Thanks again

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.