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
1 Answer
Reset to default 4Better 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
}