title: Failed to execute 'atob' on 'Window'
body: <p>I'm trying to save my HTML file in Chrome when the user presses <code>ctrl + s</code> keys but Chrome is crashed.</p>
  
  <p>(I want to download just the source code of my HTML file)</p>
  
  <p>I read that it happens because my file is bigger than 1.99M..</p>
  
  <p><strong>In the first attempt (before I knew about the crashing in Chrome):</strong></p>
  
  <pre><code>function download(filename, text) {
      var pom = document.createElement('a');
      pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
      pom.setAttribute('download', filename);
      pom.click();
  }
  
  download('test.html', "&lt;html&gt;" + document.getElementsByTagName('html')[0].innerHTML + "&lt;/html&gt;");
  </code></pre>
  
  <p><strong>The second attempt, after I read about the crashing, I used <code>blob</code>:</strong></p>
  
  <pre><code>function dataURItoBlob(dataURI) {
      var byteString = atob(dataURI.split(',')[1]);
  
      var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]
  
      var ab = new ArrayBuffer(byteString.length);
      var ia = new Uint8Array(ab);
      for (var i = 0; i &lt; byteString.length; i++) {
          ia[i] = byteString.charCodeAt(i);
      }
  
      var bb = new BlobBuilder();
      bb.append(ab);
      return bb.getBlob(mimeString);
  }
  
  function download(dataURI) {
      var blob = dataURItoBlob(dataURI);
      var url  = window.URL.createObjectURL(blob);
      window.location.assign(url);
  }
  
  download("&lt;html&gt;" + document.getElementsByTagName('html')[0].innerHTML + "&lt;/html&gt;")
  </code></pre>
  
  <p>Here I got the error: <code>Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.</code></p>
  
  <p>I don't know, but I read that I need to encode my string to base64: <a href="https://stackoverflow.com/questions/246801/how-can-you-encode-a-string-to-base64-in-javascript">How can you encode a string to Base64 in JavaScript?</a></p>
  
  <p>There is an answer of 148 votes. I paste it in my code and don't know how to continue.</p>
  
  <p>Where should I call it and how? Can I put a name on my saved file?</p>
  
  <p>I think that I need to do something like:</p>
  
  <pre><code>download(_utf8_decode("&lt;html&gt;" + document.getElementsByTagName('html')[0].innerHTML + "&lt;/html&gt;"))
  </code></pre>