title: JS: Download zip file from a string with Javascript
body: <p>I want to receive a zip file as a string from an Ajax request and then hold it in memory so it can be downloaded multiple times if necessary so that only the one request is sent.</p>
  
  <p>I tried to download it with this:</p>
  
  <pre><code>zip_string = 'PK etc.'
  
  function download(filename, text) {
    var element = document.createElement('a');
    element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
    element.setAttribute('download', filename);
  
    element.style.display = 'none';
    document.body.appendChild(element);
  
    element.click();
  
    document.body.removeChild(element);
  }
  
  // Start file download.
  download("zip1.zip", zip_string);
  </code></pre>
  
  <p>It came through as a zip but then there was obviously a problem because it wouldn't open. Can anyone see what I'm doing wrong?</p>