1

I have a silly issue with a an angularjs application. The setup is that I have a REST API which I have built an angularjs frontend for. The REST API uses HTTP basic authentication. I now want to allow the user download a file provided from the REST interface.

I can download the file using the $resource or the $http service which is great but I just end up with the data as blob in a javascript variable. I can't forward the file so the browser pops up the file download dialog allowing the user to download it. The other solution I thought of was forwarding the browser directly to the REST api URL for the file but I don't know how to inject the basic HTTP credentials into the header so I will get an error rather than have the file downloaded.

Do anyone know how to solve this issue?

UPDATE

I solved the issue using the duplicate question link provided. FileSaver.js did the trick for me!

Laserallan
  • 11,072
  • 10
  • 46
  • 67
  • possible duplicate of [Using HTML5/Javascript to generate and save a file](http://stackoverflow.com/questions/2897619/using-html5-javascript-to-generate-and-save-a-file) – Daniel Jul 13 '14 at 00:12

1 Answers1

1

To show the Blob to the user you might insert it into a HTML Element of your page. You might simply add {{variableNameofYourResult}} somewhere within your controller.

Edit: To invoke a file download you could use the Downloadify library (which has additional dependencies like flash 10)

For another possible solution without the flash denpendency have a look at this blog post.

Daniel
  • 3,541
  • 3
  • 33
  • 46
  • In order for this to work I would need to put the {{}} into an empty page except for the content type. As far as I know I won't be able to execute angularjs javascript in that page. – Laserallan Jul 12 '14 at 23:26
  • so, you don't want the page to contain anything else beyond that rest result? – Daniel Jul 12 '14 at 23:42
  • I want the download file dialog pop up so the user can store it locally. It's a binary file that doesn't make any sense showing in a web page. – Laserallan Jul 12 '14 at 23:45
  • I flagged a duplicate question as the answers over there should help to solve your problems. If flash is an option for you you definitely should have a look at Downloadify. – Daniel Jul 13 '14 at 00:13
  • Thanks, that question seem to outline the options! – Laserallan Jul 13 '14 at 00:18
  • I ended up using FileSaver.js. Thanks for pointing me in the right direction! – Laserallan Jul 13 '14 at 01:28