C# 使用AJAX和ASP.NET MVC发送/接收字节[]

C# 使用AJAX和ASP.NET MVC发送/接收字节[],c#,jquery,ajax,asp.net-mvc,C#,Jquery,Ajax,Asp.net Mvc,概述:这里是对我所做工作的总结描述-我有一个不断运行的C#应用程序,其中包含一个HTTPListener并等待请求。还有一个MVC Web应用程序,其中一个页面上有一个按钮,该按钮触发一些JS,向HTTP侦听器正在侦听的地址发送ajax post,当单击该按钮时,侦听器捕获该请求,然后C#应用程序执行一些其他工作(与问题无关),然后生成一个字节数组。然后,该字节数组作为对原始请求的响应的一部分发送回来,因此发出请求的ajax post的success函数将使用success函数的数据参数中的字节

概述:这里是对我所做工作的总结描述-我有一个不断运行的C#应用程序,其中包含一个HTTPListener并等待请求。还有一个MVC Web应用程序,其中一个页面上有一个按钮,该按钮触发一些JS,向HTTP侦听器正在侦听的地址发送ajax post,当单击该按钮时,侦听器捕获该请求,然后C#应用程序执行一些其他工作(与问题无关),然后生成一个字节数组。然后,该字节数组作为对原始请求的响应的一部分发送回来,因此发出请求的ajax post的
success
函数将使用success函数的数据参数中的字节数组触发。现在又发布了另一篇ajax文章,但这次发布到MVC控制器,以便字节数组可以保存在my db中:

下面是一些代码

首先,这里是向HTTP侦听器发出请求的ajax post,随post一起传递的数据只是我使用的一些元数据(现在忽略success函数中的代码):

下面是HTTP侦听器的回调方法,它捕获请求并发送响应:

var context = listener.EndGetContext(listenerresult);
Thread.Sleep(1000);
var data_text = new StreamReader(context.Request.InputStream,context.Request.ContentEncoding).ReadToEnd();

//functions used to decode json encoded data.
JavaScriptSerializer js = new JavaScriptSerializer();
RequestConfiguration RequestConfig = (RequestConfiguration)js.Deserialize(data_text, typeof(RequestConfiguration));

byte[] templateDataArray = null;

//Do some work and assign value to the byte array, 'templateDataArray'

//I append a status code and a message to the array of bytes and send everthing back as a response
//The values are split using the '<>' characters.
byte[] msgDataArray = System.Text.Encoding.UTF8.GetBytes("1<>Success" + "<>");

byte[] responseArray = new byte[msgDataArray.Length + templateDataArray.Length];
msgDataArray.CopyTo(responseArray, 0);
templateDataArray.CopyTo(responseArray,msgDataArray.Length);

var response = context.Response;
response.ContentLength64 = responseArray.Length;
response.Headers.Add("Access-Control-Allow-Origin", "*");
response.Headers.Add("Access-Control-Allow-Methods", "POST, GET");
response.StatusCode = 200;
response.StatusDescription = "OK";
response.OutputStream.Write(responseArray, 0, responseArray.Length);
response.OutputStream.Close();
我关心什么,我的问题是什么 为什么我从监听器发送的数组与我最终在MVC控制器中读取的数组不同?我这么说的原因是,下面是从侦听器发送的字节数组:

这是MVC控制器接收到的阵列:

正如您所看到的,MVC控制器的操作在一个字符串中执行,该字符串被分成两部分,第二部分被转换为字节数组,第二部分是从侦听器接收的字符串形式的字节数组(再看一下第二篇ajax文章,您将看到它)


我认为我采用的发送和接收字节数据的方法不正确。

您可以将
templateDataArray
解析为base64字符串,然后在两侧运行比较。如果它们是相同的,.NET framework可能会产生一些开销。如果没有,您可能会发现确切的区别是。看看binary.js。如果.Net增加了开销,我该如何补偿?
var context = listener.EndGetContext(listenerresult);
Thread.Sleep(1000);
var data_text = new StreamReader(context.Request.InputStream,context.Request.ContentEncoding).ReadToEnd();

//functions used to decode json encoded data.
JavaScriptSerializer js = new JavaScriptSerializer();
RequestConfiguration RequestConfig = (RequestConfiguration)js.Deserialize(data_text, typeof(RequestConfiguration));

byte[] templateDataArray = null;

//Do some work and assign value to the byte array, 'templateDataArray'

//I append a status code and a message to the array of bytes and send everthing back as a response
//The values are split using the '<>' characters.
byte[] msgDataArray = System.Text.Encoding.UTF8.GetBytes("1<>Success" + "<>");

byte[] responseArray = new byte[msgDataArray.Length + templateDataArray.Length];
msgDataArray.CopyTo(responseArray, 0);
templateDataArray.CopyTo(responseArray,msgDataArray.Length);

var response = context.Response;
response.ContentLength64 = responseArray.Length;
response.Headers.Add("Access-Control-Allow-Origin", "*");
response.Headers.Add("Access-Control-Allow-Methods", "POST, GET");
response.StatusCode = 200;
response.StatusDescription = "OK";
response.OutputStream.Write(responseArray, 0, responseArray.Length);
response.OutputStream.Close();
 ...
 success: function (data) {

 //Code that posts array to server to be saved in DB

 var returnArray = data.split("<>");

 if (returnArray[0] === "1") {
  alert(returnArray[1]);
  $("#Loader").addClass('hide');

  $.ajax({
    type: "POST",
    url: '@Url.Action("EnrollFingerprintToDB")',
    dataType: 'json',
    data: { enrollData: NewID + '<>' + returnArray[2] },
    success: function (data) {

      alert(data.msg);

    }

  });//end of inner ajax

 } //end of if

} //end of success
...
[HttpPost]
public ActionResult EnrollFingerprintToDB(string enrollData)
{
    string[] sDataParts = enrollData.Split(new[] { "<>" }, StringSplitOptions.None);

    var bytes = System.Text.Encoding.UTF8.GetBytes(sDataParts[1]);

    if (FingerprintBLL.InsertFingerprintTemplate(int.Parse(sDataParts[0]),bytes))
    {
        return Json(new { success = true, msg = "Template successfully saved" });
    }
    else
    {
        return Json(new { success = true, msg = "Template could not be saved" });    
    }

}