title: Download Binary Files with Javascript
body: <p>I want to download binary files using Javascript.</p>
  
  <p>I have a REST service that returns the binary data and i want to know if its possible to show the binary file, whichever the file extension.</p>
  
  <p>This is my current code:</p>
  
  <pre><code>var xhr = new XMLHttpRequest;
  xhr.open("GET", requestUrl);
  xhr.addEventListener("load", function () {
      var ret = [];
      var len = this.responseText.length;
      var byte;
      for (var i = 0; i &lt; len; i++) {
          byte = (this.responseText.charCodeAt(i) &amp; 0xFF) &gt;&gt;&gt; 0;
          ret.push(String.fromCharCode(byte));
      }
      var data = ret.join('');
      data = "data:application/pdf;base64," + btoa(data);
  
      window.open(data, '_blank', 'resizable, width=1020,height=600');
  }, false);
  
  xhr.setRequestHeader("Authorization", "Bearer " + client.accessToken);
  xhr.overrideMimeType("octet-stream; charset=x-user-defined;");
  xhr.send(null);
  </code></pre>
  
  <p>Thanks!</p>