Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 - Download File on Click Event

I am unable to find the answer anywhere, hopefully someone can help.

I have an Angular2 click event defined in the template, inside the table's cell:

<td (click)="click2Download()">fileName.txt</td>

In the typescript component, I need help to define a method click2Download() that would automatically start downloading the fileName.txt to client's browser. Note: file is on the same domain, e.g.:

http://example.com/fileName.txt

Any way of doing this with Angular2?

like image 915
jjj Avatar asked Nov 02 '16 00:11

jjj


1 Answers

Have you tried: window.location.href = '...'; ?

To force download of files you should use html attribute: <a target="_self" href="somefile.txt" download="somefile.txt">

like image 129
spa900 Avatar answered Sep 28 '22 04:09

spa900