javascript 如何使用 AJAX 将字节数组发送到服务器端
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16939244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send byte array to server side using AJAX
提问by
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
如果这个问题太愚蠢或已经问过,请原谅我。我在谷歌上搜索了很多,但没有得到我想要的任何东西。我需要使用 ajax 将字节数组传递给服务器端,但它没有按计划工作我当前的代码如下
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
我知道我的程序有问题,但我找不到请帮我解决这个问题
采纳答案by Artyom Neustroev
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
}