0

I have an Web API end point which returns location of URL (http://somedomain.com/myproj/reports/dailystock.pdf).

A button is click, then this API gets called, url is returned).It might be PDF or XL or Word file too

How can i want to download the file (save on) to disk using JavaScript (AngularJs).

At present, I open the file in new window (tab).

Mithun Pattankar
  • 1,372
  • 1
  • 8
  • 12
  • Opening the file in a new window should open the download popup unless the browser have a builtint viewer. Why does `window.open()` not sufice? – Shilly May 08 '18 at 13:28

1 Answers1

0

You can use FileSaver.

Add this as a dependency to your module

.module('module', ['ngFileSaver'])

Use it like

 $http.post('YourAPIURL', {optionsObject}, { responseType: 'arraybuffer' })
  .success(function(data) {
            var file = new Blob([data], { type: 'application/pdf' });
            saveAs(file, 'filename.pdf');
        });

Read here and this would also be useful

Muhammad Usman
  • 10,039
  • 22
  • 39