Gecko (Firefox)、WebKit (Safari、Chrome) 和 Opera 支持以 64 进制编码字符串的非标准btoa
函数。为了获得包含编码为 UTF-8 的字符串的 64 进制字符串,您需要使用encodeURIComponent
-unescape
技巧. encodeURIComponent
将字符串编码为 UTF-8 URL,但将unescape
每个字符串解码%xx
为单个字符。btoa
期望您想要的任何编码的二进制字符串。
var base64 = btoa(unescape(encodeURIComponent(data)));
window.open("data:text/plain;charset=UTF-8;base64,"+base64,"UTF-8 Text");
当然这在 IE 中不起作用,但我认为 IE 10 将支持Blob
-API。谁知道它将如何处理编码。
PS:IE 似乎无法window.open
data:-urls 并且无论如何都会有一个荒谬的小 url 长度限制。
PPS:这在 Chrome 中适用于我:
var b = new Blob(["➀➁➂ Test"],{encoding:"UTF-8",type:"text/plain;charset=UTF-8"});
var url = URL.createObjectURL(b);
window.open(url,"_blank","");