title: How to encode UTF8 characters into Base64 in JavaScript
body: <p>I tried like this when handling chinese, but the output is <strike>messy code</strike> garbled:</p>
  
  <pre><code>var a = "你好";
  undefined
  a
  "你好"
  a = unescape(encodeURIComponent(a));
  "ä½ å¥½"
  a
  "ä½ å¥½"
  </code></pre>
  
  <p>compared to handling English:</p>
  
  <pre><code>var a = "Hello";
  undefined
  a
  "Hello"
  a = unescape(encodeURIComponent(a));
  "Hello"
  </code></pre>
  
  <p>Here is my whole code:</p>
  
  <pre><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;
  </code></pre>
  
  <p><strong>UPDATED:</strong></p>
  
  <p>What I am trying to do is encoding a generated SVG as data url for user to download. I solved this problem by converting SVG to canvas (using CanVG) first and use html2canvas to generate base64 ... finally it works ...</p>