4

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

12
  • 1
    What does "Not working as planned" mean? Are you getting any errors on the page or in your server-side logs? Commented Jun 5, 2013 at 11:55
  • @AndrewWhitaker no I did't get any errors actually the ajax function was called but it did not invoke the server side method Commented Jun 5, 2013 at 11:58
  • Your not implementing array correctly. It should be something like this: var mycars = new Array(); mycars[0] = "Saab"; mycars[1] = "Volvo"; mycars[2] = "BMW"; When you check array length - use <name_of_array>.length Commented Jun 5, 2013 at 11:58
  • 1
    @Nilzone- There is no problem with the array the array is in correct structure Commented Jun 5, 2013 at 12:00
  • Can you post your Upload action method? Commented Jun 5, 2013 at 12:01

1 Answer 1

4

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
    }
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.