Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs simple file download causes router to redirect

Tags:

angularjs

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?

like image 823
UpCat Avatar asked May 21 '13 13:05

UpCat


People also ask

What is use of$ routeProvider in AngularJS?

$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.

How routing works in AngularJS?

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.

How to use ngRoute in AngularJS?

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 .


1 Answers

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"> 
like image 186
jessegavin Avatar answered Oct 13 '22 04:10

jessegavin