title: Use the base64 preview of the binary data response (zip file) in angularjs
body: <p>I always get this error in the downloaded zip file <code>C:\Users\me\Downloads\test.zip: Unexpected end of archive</code></p>
  
  <p>My current code is:</p>
  
  <pre><code>var blob = new Blob([data], { // data here is the binary content
      type: 'octet/stream',
  });
  var zipUrl = window.URL.createObjectURL(blob);
  var fileName = orderNo;
  fileName += '.zip';
  downloadFile(null, fileName, null, zipUrl, null); // just creates a hidden anchor tag and triggers the download
  </code></pre>
  
  <p>The response of the call is a binary (I think). <a href="https://pastebin.com/uueRgQWb" rel="nofollow noreferrer" title="Binary Content">Binary Content Here</a></p>
  
  <p>But the preview is a base64. <a href="https://pastebin.com/aQimdF8b" rel="nofollow noreferrer" title="Base64 Content">Base64 Content</a>. And it is the correct one. The way I verify it is by using this <a href="http://jsfiddle.net/VB59f/544/" rel="nofollow noreferrer" title="Fiddle">fiddle</a>.</p>
  
  <p>You can refer to the screenshot of the network <a href="https://imgur.com/a/oaUkF" rel="nofollow noreferrer" title="base64 binary">here</a></p>
  
  <p>I put the base64 content in this line <code>var sampleBytes = base64ToArrayBuffer('');</code> And the zip downloaded just opens fine.</p>
  
  <p>Things I have tried so far.</p>
  
  <ol>
  <li><p>Adding this headers to the GET call</p>
  
  <p>var headers = {
      Accept: 'application/octet-stream',
      responseType: 'blob',
  };
  But I get <code>Request header field responseType is not allowed by Access-Control-Allow-Headers in preflight response.</code></p></li>
  </ol>
  
  <p>We're using an already <code>ajax.service.js</code> in our AngularJS project.</p>
  
  <ol start="2">
  <li><a href="https://stackoverflow.com/a/23452051/4620773">From this answer</a></li>
  </ol>
  
  <p><code>var blob = new Blob([yourBinaryDataAsAnArrayOrAsAString], {type: "application/octet-stream"});
  var fileName = "myFileName.myExtension";
  saveAs(blob, fileName);</code></p>
  
  <p>There are other things that I have tried that I have not listed. I will edit the questions once I find them again</p>
  
  <p>But where I'm current at right now. The <code>preview</code> is correct base64 of the binary file. Is it possible to use that instead of the binary? (If it is I will not find the other methods that I've tested) I tried some <code>binary to base64</code> converters but they don't work.</p>