0

I wanted to download file, I am new in angular, so I can't understand what do I do

<img src="assets/images/download.png" alt="IMG-LOGO" height="20" width="20" (click)="getfile(product.product_id)">

and in ts file I wrote function for api call

getfile(id){

        this.filter.productid = id;
        this.apiService.getfile(this.filter).subscribe((data: Array<object>) => {
            this.fileUrl = data;
            console.log(data);
        });

    }
Cœur
  • 37,241
  • 25
  • 195
  • 267
Tejaswee
  • 149
  • 2
  • 3
  • 16
  • Possible duplicate of [How download a file from HttpClient](https://stackoverflow.com/questions/51682514/how-download-a-file-from-httpclient) – mumair Feb 22 '19 at 06:24

2 Answers2

1

Why do you need to leverage angular to trigger the file download? HTML can do this without any help... Just change your code to something like this:

<a href="/path/to/file/{{ product.product_id }} ">
    <img src="assets/images/download.png" alt="IMG-LOGO" height="20" width="20">
</a>
Michael Miller
  • 399
  • 1
  • 9
  • Yes, although, you may have to alter the headers from the server-side code to force the download, but from the front end, this is how to do it. – Michael Miller Feb 22 '19 at 06:27
  • Also, if you reference @zmag post above, he mentions using 'target="_blank"'... That can't hurt, either. – Michael Miller Feb 22 '19 at 06:28
  • And to add extra clarity... You CAN initiate a download with JavaScript, but since you have to implement this using extra code for something HTML does out of the box, don't do it... – Michael Miller Feb 22 '19 at 06:29
  • I have no idea what language/framework your server side is written with... – Michael Miller Feb 22 '19 at 17:17
  • In that case, I don't know how you're managing files within Codeigniter, which web server you're running, whether your web server is set up correctly, whether your file permissions are set correctly, etc. This is a completely different problem than getting files to download using AngularJS. My suggestion would be to mark this question as solved, and post a different problem in the CodeIgniter channel. – Michael Miller Feb 24 '19 at 18:48
-1

Your goal is just to download a file, you don't have to struggle with Angular. simply link it with a tag.

<a href="/download/id" target="_blank">
  <img src="assets/images/download.png" alt="IMG-LOGO" height="20" width="20">
</a>
zmag
  • 7,825
  • 12
  • 32
  • 42
  • it's not working i am getting blank output, i can not download it – Tejaswee Feb 22 '19 at 06:50
  • just using an href also won't work if you have a protected api. And this href is pointing to a root relative path, but the example above is using an api service... – seawave_23 May 09 '19 at 16:26