title: How do you handle download link that requires Authorization token using Angular?
body: <p>I have a REST API that I'm using alongside Angular. Currently I have post objects that have file attachments. To download each file attachment, a call must be made to /get/file/{id}, but an authorization token is required each time to convey the user is logged in and has proper rights.</p>
  
  <p>After much searching, I ended up with the following setup using <a href="https://github.com/alferov/angular-file-saver" rel="nofollow">Angular-File-Saver</a> (which uses FileSaver.js and Blob.js):</p>
  
  <p>feed.html</p>
  
  <pre><code>&lt;p class="post-attachments text-right" ng-repeat="file in post.files"&gt;
   &lt;button type="button" class="btn-link" ng-click="feed.getFile(file)"&gt;{{file.name}}&lt;/button&gt;                        
  &lt;/p&gt;
  </code></pre>
  
  <p>feed.controller.js</p>
  
  <pre><code>function getFile(file) {
      var file_id = file.id;
  
  PostService.GetFile(file_id).then(function(response) {
      var file_type = response.headers('Content-Type');
      var data = new Blob([response.data], {type: file_type});
  
      FileSaver.saveAs(data, file.name);
  }, function error(response) {
      console.log(response);
  });
  </code></pre>
  
  <p>}</p>
  
  <p>While this sort-of works, there's a few big problems. Because this is different from just using an anchor link like <code>&lt;a href="link.com/assets/stockphoto.jpeg" download&gt;&lt;/a&gt;</code>, it's really a hack on downloading a file and results in poor browser support. Safari and mobile iOS don't currently work with this solution (Safari opens the file in a new window using blob:<a href="http://siteaddress.com" rel="nofollow">http://siteaddress.com</a> and mobile Safari doesn't even load the file).</p>
  
  <p>Additionally, there's no browser support for a progress indication. This means clicking the download doesn't actually do anything until the API returns the entire download, where the file is then shown in the browser downloads section. This results in wait times without any indication of what's happening.</p>
  
  <p>What I'm wondering is this--is there a better way to handle this problem besides not requiring an authorization token each time the /get/file/{id} endpoint is called? I'm bashing my head against the wall here since this seems like a common issue but has been difficult to find answers on.</p>