HTML:
<a href="mysite.com/uploads/asd4a4d5a.pdf" download="foo.pdf">
Uploads get a unique file name while there real name is kept in database. I want to realize a simple file download. But the code above redirects to / because of:
$routeProvider.otherwise({ redirectTo: '/', controller: MainController });
I tried with
$scope.download = function(resource){ window.open(resource); }
but this just opens the file in a new window.
Any ideas how to enable a real download for any file type?
$routeProvider is used to configure the routes. We use the ngRoute config() to configure the $routeProvider. The config() takes a function which takes the $routeProvider as parameter and the routing configuration goes inside the function.
Routing in AngularJS is used when the user wants to navigate to different pages in an application but still wants it to be a single page application. AngularJS routes enable the user to create different URLs for different content in an application.
Then you must add the ngRoute as a dependency in the application module: var app = angular. module("myApp", ["ngRoute"]); Now your application has access to the route module, which provides the $routeProvider .
https://docs.angularjs.org/guide/$location#html-link-rewriting
In cases like the following, links are not rewritten; instead, the browser will perform a full page reload to the original link.
Links that contain target element Example:
<a href="/ext/link?a=b" target="_self">link</a>
Absolute links that go to a different domain Example:
<a href="http://angularjs.org/">link</a>
Links starting with '/' that lead to a different base path when base is defined Example:
<a href="/not-my-base/link">link</a>
So in your case, you should add a target attribute like so...
<a target="_self" href="example.com/uploads/asd4a4d5a.pdf" download="foo.pdf">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With