How to get directions / links to a PDF file in Angular 2?

I am trying to extract a client side PDF file for display in a browser. I tried my usual href path with respect to the html file, and it does not work.

<a href="../assets/file.pdf>Get File</a> 

And using routing doesn't work either, or I'm not using the correct tag ...

 RouterModule.forRoot([ { path:"resume", redirectTo: '../assets/file.pdf' }, { path: '', component: AppComponent } ]) 

Not sure if routing is the way to go, I just learned how to route components based on their documentation.

+7
source share
2 answers

<a href=".. should work. I think the problem is the path for your pdf file. Have you tried <a href="/assets/file.pdf> instead of ../assets ?

+8
source

I have another solution. I use this in my project:

 { path: 'assets/file.pdf', redirectTo: '../assets/file.pdf'} 

and I use routerLink to get the file as follows:

 <button class="btn-style" routerLink="/assets/file.pdf" routerLinkActive="active">Get File</button> 

Hope this helps someone :), works with Angular 6 too.

0
source

All Articles