[英]How to encode UTF8 characters into Base64 in JavaScript
I tried like this when handling chinese, but the output is
messy code
garbled: 我在处理中文时尝试过这样,但输出的
代码是
乱码:
var a = "你好";
undefined
a
"你好"
a = unescape(encodeURIComponent(a));
"ä½ å¥½"
a
"ä½ å¥½"
compared to handling English: 与处理英语相比:
var a = "Hello";
undefined
a
"Hello"
a = unescape(encodeURIComponent(a));
"Hello"
Here is my whole code: 这是我的整个代码:
var content = $("div#test").html();
content = unescape(encodeURIComponent( content ));
content = window.btoa(content);
content = "data:image/svg+xml;filename:{{ request.session.access_token.uid }}.svg;base64," + content;
UPDATED: 更新:
What I am trying to do is encoding a generated SVG as data url for user to download. 我想要做的是将生成的SVG编码为用户下载的数据URL。 I solved this problem by converting SVG to canvas (using CanVG) first and use html2canvas to generate base64 ... finally it works ...
我通过首先将SVG转换为canvas(使用CanVG)并使用html2canvas生成base64来解决这个问题...最后它可以...
Try roundtripping this, and it works fine: 尝试往返这个,它工作正常:
decodeURIComponent(escape(window.atob(window.btoa(unescape(encodeURIComponent("東京"))))))
Whether or not some intermediate result appears to be garbled is not relevant. 某些中间结果是否显示为乱码是不相关的。
The problem with the window.btoa(decodeURIComponent(escape(unescape(encodeURIComponent("東京")))))
in one of the comments is the calls are not balanced and not in the right order. window.btoa(decodeURIComponent(escape(unescape(encodeURIComponent("東京")))))
中的一个注释的问题是调用不平衡而且顺序不正确。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.