简体   繁体   中英

Downloading file from REST interface from angularjs application

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!

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM