Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File download through Angular 2

I have a backend that I set up to return a file through setting the header

Content-Disposition: attachment;filename=somefile.csv

It works directly in the browser and downloads the file immediately upon invoking the URL that points to that resource.

My goal is to have a button in an Angular 2 template. When the user clicks that button, I'd need to collect some data from the client-side (some IDs) and send it to the server to invoke that file download URL.

I'd like the user to stay on the same page and not have any new tabs open up but simply have the file start downloading (just like when the URL is invoked directly).

It will need to be done through a POST request because I can have quite a bit of data to send to identify what resource needs to be downloaded.

What does the call on the Angular 2 side look like for this? I tried a couple of things but I am obviously on the wrong path.

Any help would be appreciated!

like image 268
user1902183 Avatar asked Mar 26 '17 13:03

user1902183


People also ask

How to download a file from Angular?

Angular Download Link You'll use an anchor tag pointing to the file with the href attribute. The download attribute informs the browser that it shouldn't follow the link but rather download the URL target. You can also specify its value in order to set the name of the file being downloaded.

How to download a file from URL in Angular?

All you should need to do is to create a link to the file (place the URL you're using in the AJAX request in the href of the link instead) and then have your Express code "strongly encourage" the client to download the file rather than display it.

How do you download a file using typescript?

import fs = require("fs"); import { HttpClient } from "typed-rest-client/HttpClient"; async function run() { const client = new HttpClient("clientTest"); const response = await client. get("https://some.server.of.mine.com/largeImage.png"); const filePath = "C:\\temp\\downloadedFile.

How do I download a file from API?

In this article, I will use a demo Web API application in ASP.NET Core to show you how to transmit files through an API endpoint. In the final HTML page, end users can left-click a hyperlink to download the file or right-click the link to choose “ Save Link As ” in the context menu and save the file.


1 Answers

i have implemented it like this.

i have a service requesting file download. The response return a url, which is on amazon s3. This is a zip file containing what i want to download.

the below works on all browsers.

in your controller

requestDownload() {

this.downloadservice.downloadImages(obj)
      .subscribe(
        response => this.download(response )
      );

}

// res is an object
download(res){ 
    var link = document.createElement("a");
    link.download = "a";
    link.href = res.link;
    document.body.appendChild(link);
    link.click();

     }

downloadservice file

downloadImages(body): Observable<any>{

        let headers = new Headers({ 'Content-Type': 'application/json' });
        let options = new RequestOptions({ headers: headers });
        return this.http.post("/Camera51Server/getZippedImages", body, options)
            .map((res:Response) => res.json())
            .catch(this.handleError);
    }

if you like i can give you a link to the repository.

like image 96
Roninio Avatar answered Sep 24 '22 03:09

Roninio