简体   繁体   中英

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

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
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM