title: Writing UTF-8 to a file and downloading it with FileSaver.js
body: <p>I've been working on making a Distributed File System and have come to a halt today with trying to write the contents of a returned file for the browser to download.</p>
  
  <p>My system sends a request for a file's data and gets a string of the entire UTF-8 encoded binary contents as a response. I'm then trying to use <a href="https://github.com/eligrey/FileSaver.js/" rel="nofollow noreferrer">FileSaver.js</a> to write and download this content.</p>
  
  <p>It only works for .txt files at the moment. I suspect it's something to do with the encoding and how the Blob object is made, I think it's simply writing the UTF-8 as text to each file which is why it works for .txt and nothing else. I've tried converting the response to byteArrays or using a File object instead but nothing works.</p>
  
  <p>Here's the code doing the request and handling the response:</p>
  
  <pre><code>$.ajax({
    type: "POST",
    url: "http://localhost:3040/read",
    data: {
      data: data
    },
    xhrFields: {
      withCredentials: true
    },
    success: function(response) {
      var FileSaver = require('file-saver');
      var blob = new Blob([response], {type: "application/octet-stream"});
      FileSaver.saveAs(blob, filename);
    }
  });
  </code></pre>
  
  <p>An small snippet of the response output (in this case for a png image):</p>
  
  <pre><code>PNG
  \u1a
  \u0\u0\u1a
  IHDR\u0\u0\u2î\u0\u0\u56Eµ¤Q\u0\u0\u0\u1sRGB\u0®Î\u1cé...
  </code></pre>