I'm trying to make JavaScript download a unicode string as a text file. I'm at the point where I need to convert the unicode string into a data URL, so that the user can open the URL and download the file. Here is a simplification of my code:
var myString = "⌀怴ꁴ㥍䯖챻巏ܛ肜怄셀겗孉贜짥孍ಽ펾曍㩜䝺捄칡⡴얳锭劽嫍ᯕ�";
var link = document.createElement('a');
link.setAttribute('href', 'data:text/plain;base64,' + myString);
I don't know what character set to use or how to encode my string - I've tried combinations of encodeURI()
and btoa()
, but haven't managed to get anything working. encodeURI()
gives me the error Uncaught URI Error: malformed URI
for some characters like U+da7b
.
I would prefer the final downloaded file to have the same characters as the initial string.
try
/catch
block to isolate failures.btoa
gets clean input.