user1589906
user1589906

Reputation:

How to send byte array to server side using AJAX

forgive me if this question is too silly or already asked I googled a lot but I didnt get anything I want. I need to pass a byte array to server side using ajax but its not working as planned my current code is given below

var bytes = [];

for (var i = 0; i < data.length; ++i) {
    bytes.push(data.charCodeAt(i));
}

$.ajax({
    url: '/Home/ImageUpload',
    dataType: 'json',
    type: 'POST',
    data:{ data:bytes},
    success: function (response) {
        alert("hi");
    }
}); 

Upload Method

    [HttpPost]
    public ActionResult ImageUpload(byte[] data)
    {
                ImageModel newImage = new ImageModel();
                ImageDL addImage = new ImageDL();
                newImage.ImageData = data;
                addImage.AddImage(newImage);
                return Json(new { success = true });

    }

I know something wrong with my program but I cant find it please help me to solve this

Upvotes: 4

Views: 22106

Answers (1)

Artyom Neustroev
Artyom Neustroev

Reputation: 8715

Better do this:

$.ajax({
    url: '/Home/ImageUpload',
    dataType: 'json',
    type: 'POST',
    data:{ data: data}, //your string data
    success: function (response) {
        alert("hi");
    }
}); 

And in controller:

[HttpPost]
    public ActionResult ImageUpload(string data)
    {
        var bytes = System.Text.Encoding.UTF8.GetBytes(data);
        //other stuff
    }

Upvotes: 4

Related Questions