To convert a JavaScript string into an ArrayBuffer, you can utilize a legacy polyfill for the native TextEncoder API. The TextEncoder documentation specifies support for UTF16 with different endianness. It is likely that libraries offering UTF-16 support in a Text-Encoder-compatible manner will emerge soon, if they haven't already. For instance, let's imagine there's a library with a constructor named ExtendedTextEncoder
.
You can then generate a Blob URL to facilitate file downloads for users, bypassing the cumbersome base-64 conversion process.
Here's an example:
s = "aosjdfkzlzkdoaslckjznx"
var encoder = new ExtendedTextEncoder("utf-16be")
var blob = new Blob(encoder.encode(s), "text/plain")
var url = URL.createObjectURL(blob)
From now on, you can utilize url
instead of your data:
URL.