1

i have implemented a controller like this:

[HttpPost]
[ActionName("DefaultAction")]
public HttpResponseMessage Post(Person obj){
}

Person is this class:

public class Person
{
   public String Name { get; set; }
   public byte[] ImageStream { get; set; }
}

At client side i make this call to post new Person:

var person={
            "Name":"Test",
            "ImageStream":"AQID"
}

$.ajax({
            type: "POST",
            url: "/person",
            dataType: "json",
            contentType: "application/json",
            data: JSON.stringify(person),
            success: function (result) {
                console.log(result); //log to the console to see whether it worked
            },
            error: function (error) {
                alert("There was an error posting the data to the server: " + error.responseText);
            }
});

The problem is that i receive the Person obj with ImageStream set as null.

To test if ImageStream string that i use is correct i tried this:

Person p=new Person();
p.Name="Test";
p.ImageStream=new byte[]{1,2,3};
String json=JsonConvert.SerializeObject(p);

1 Answer 1

2

In your Javascript code you are not passing a byte array to your C# method, you're passing a string. They are not the same thing. If you want to pass a byte array, it needs to be an actual array of numbers whose values are between 0 and 255.

Try it like this:

var person = {
    "Name": "Test",
    "ImageStream": [65,81,73,68]
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, no it doesn't works. It works only sometimes, my solutions too, works only sometimes and i don't understand why.

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.