Thursday, June 29, 2017

Fun fact: Did you know that a bytes array representation of a file in C# when consumed from a REST endpoint will become a string in JSON/JavaScript?

A .txt file containing "Hello World" and nothing else makes for a bytes array with this in it when consumed in C#:

  1. 72
  2. 101
  3. 108
  4. 108
  5. 111
  6. 32
  7. 87
  8. 111
  9. 114
  10. 108
  11. 100

The values here I assume correspond to applicable ASCII (American Standard Code for Information Interchange) characters. Anyhow, when you get this in JavaScript, you'll just have: SGVsbG8gV29ybGQ=

 
 

By the way, this is how you'd barf back a file from this data on the JavaScript side from a base64 string. I am stealing this from here and, yes, it works.

var byteCharacters = atob("SGVsbG8gV29ybGQ=");
var byteNumbers = new Array(byteCharacters.length);
for (var i = 0; i < byteCharacters.length; i++) {
   byteNumbers[i] = byteCharacters.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
var blob = new Blob([byteArray], {type: "text/plain"});
var fileURL = URL.createObjectURL(blob);
window.open(fileURL);

No comments:

Post a Comment