NoWhereToBeSeen
NoWhereToBeSeen

Reputation: 1444

Byte array from backend is converted to something else on front-end

[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;
}

when I hover over with mouse it shows following bytes (which is correct): enter image description here

But when I check on the front-end (javascript). It becomes a different arrayBuffer:

enter image description here

Here is the front end code:

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();

Upvotes: 1

Views: 2401

Answers (1)

Shaun Luttin
Shaun Luttin

Reputation: 141512

You did not ask a specific question, but I think this might help.

Your controller action is responding with JSON. Dumping json to the console shows the same array values on the front-end as does dumping sbt to the console on the back-end. Here is the front-end code that dumps the values.

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");
    }
};

So, you're sending a JSON array.

As an aside, here are some links about the arraybuffer response type.

Upvotes: 1

Related Questions