How to send an array of bytes to a server using AJAX

Forgive me if this question is too stupid or already asked, I googled a lot, but I got nothing. I need to transfer an array of bytes to the server using ajax, but it does not work as planned, my current code is 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"); } }); 

Download 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 is wrong with my program, but I can not find it, please help me solve this problem.

+4
source share
1 answer

Better do this:

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

And in the controller:

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

Source: https://habr.com/ru/post/1484622/


All Articles