I
I
Ihor Gevorkyan2016-05-17 12:08:53
JavaScript
Ihor Gevorkyan, 2016-05-17 12:08:53

AngularJS: How to download a PDF from a link with a download attribute?

Good day.
Faced a problem, tell me the solution.
On the back - Laravel 5.x, on the front Angular 1.5.x (I use ui-router for routing on the front).
There are PDF files that are stored on the server in the stock_files folder . You need to make a link, by clicking on it the file should be downloaded. On windows, the option provided below works, on MacOS it is redirected to:

$urlRouterProvider.otherwise("/admin/redirecthome");

I create the link like this:
<a
  ng-if="f.id"
  ng-href="stock_files/{{vm.item.id}}/{{f.name}}"
  download
  target="_blank"
  ng-bind="f.original_name">
</a>
, where vm.item.id is the id of the item, f.name is the file name.
On MacOS (Safari) errors do not appear, a new tab opens and a redirect to state 'admin.redirecthome' occurs, the file is not downloaded. On windows, a new browser tab opens, the file is downloaded, the tab closes.
I used
target="_blank"
it just in case, with the hope that if the download does not work, the file will open in the browser in a new tab. I tried to remove the download attribute and set target="_self" , it doesn't work on MacOS either.
The front config is set to:
$locationProvider.html5Mode(true);
and
<base href="/">,
so links work based on the domain name.
Who met the problem, tell me the solution, please.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question