后端的字节数组转换为前端的其他内容

时间:2018-07-06 20:58:48

标签: javascript c# asp.net-web-api asp.net-core

[Route("encrypted")]
[HttpGet]
public sbyte[] Encrypted()
{
    var mm = System.IO.File.ReadAllBytes("C:\\test\\" + "fill.txt");
    sbyte[] sbt = new sbyte[mm.Length];
    Buffer.BlockCopy(mm, 0, sbt, 0, mm.Length);
    return sbt;
}

当我将鼠标悬停在鼠标上时,它显示以下字节(正确): enter image description here

但是当我检查前端(javascript)时。它变成了另一个arrayBuffer:

enter image description here

这是前端代码:

var xhr = new XMLHttpRequest();
xhr.open('GET', '/api/encrypted/', true);
xhr.responseType = 'arraybuffer'; //i have tried without this line too
xhr.onload = function (e) {
    if (this.status === 200) {
        console.log("received from server--------");
        console.log(e.currentTarget.response);
        console.log("received from server-------");
    }
};

xhr.send();

1 个答案:

答案 0 :(得分:1)

您没有提出具体问题,但我认为这可能会有所帮助。

您的控制器操作正在以JSON响应。将json转储到控制台会在前端显示与在后台转储sbt到控制台相同的数组值。这是转储值的前端代码。

var xhr = new XMLHttpRequest();
xhr.open('GET', '/api/values', true);
xhr.responseType = 'json';
xhr.onload = function (e) {
    if (this.status === 200) {
        console.log("json");
        const json = e.currentTarget.response;
        console.log(json);
        console.log("json");
    }
};

因此,您正在发送JSON数组。

顺便说一句,这里有一些有关arraybuffer响应类型的链接。

相关问题