Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
62
rated 0 times [  67] [ 5]  / answers: 1 / hits: 21627  / 11 Years ago, wed, june 5, 2013, 12:00:00

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


More From » ajax

 Answers
65

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
}

[#77800] Tuesday, June 4, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
markusdamienn

Total Points: 167
Total Questions: 119
Total Answers: 93

Location: Oman
Member since Wed, Apr 12, 2023
1 Year ago
;