最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to send byte array to server side using AJAX - Stack Overflow

programmeradmin0浏览0评论

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

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

Share Improve this question edited Jun 5, 2013 at 12:16 asked Jun 5, 2013 at 11:52 user1589906user1589906 12
  • 1 What does "Not working as planned" mean? Are you getting any errors on the page or in your server-side logs? – Andrew Whitaker 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 – user1589906 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 – Nilzone- Commented Jun 5, 2013 at 11:58
  • 1 @Nilzone- There is no problem with the array the array is in correct structure – user1589906 Commented Jun 5, 2013 at 12:00
  • Can you post your Upload action method? – Mathew Thompson Commented Jun 5, 2013 at 12:01
 |  Show 7 more ments

1 Answer 1

Reset to default 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
    }
发布评论

评论列表(0)

  1. 暂无评论