title: Corrupted download in AngularJs app
body: <p>I am trying to download a file using <a href="https://www.google.com/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=1&amp;cad=rja&amp;uact=8&amp;ved=0CBwQFjAAahUKEwjsnOOmrLfIAhWJyRQKHZcSDDo&amp;url=https%3A%2F%2Fgithub.com%2Feligrey%2FFileSaver.js%2F&amp;usg=AFQjCNGfot02xOW1nkB4y3OnKpH3Ca2hdg&amp;sig2=sbcanYiMv93Ppa-Lk6pSGw&amp;bvm=bv.104819420,d.bGg" rel="nofollow">FileSaver.js</a>, but I get a corrupted file whenever I hit download button.</p>
  
  <p>App is backed by a PHP REST service, and using cURL from command line confirms that REST is working OK.</p>
  
  <p>Here is last version of pseudo-code that I use for downloading:</p>
  
  <pre><code>// Let str be the data received from $http promise
  // This code is run in a "then" callback
  
  var arr= new Uint8Array(str.length);
  for(var i=0; i&lt;str.length; i++) {
      arr[b]=str.charCodeAt(i);
  };
  var blob = new Blob([arr], {type: 'application/octet-stream'});
  saveAs(blob, "AFileName");
  </code></pre>
  
  <p>It just corrupts file.</p>
  
  <p>I also tried without applying the <code>Uint8Array</code>, and giving <code>str</code> directly to <code>Blob</code>. As you guessed, it failed too.</p>
  
  <p>I am writing server and client myself, so I can change anything in them. But the thing is I want to handle downloads client-side, and I just can't redirect users to file URL to download it, because REST needs authentication and that token only lives within Angular app. It would not be a good choice to change server just to handle file downloads without authentication. </p>
  
  <p>The REST has URLs like <code>/files/:id</code> that when accessed, gives file content as a regular HTTP file download (tested with cURL as said above). Could <code>$http</code> be the problem? Somehow forcing some kind of encoding or some assumptions on received data. I don't have much experience in Angular, anyway.</p>