Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to download a pdf file from an url in angular 5

Tags:

angular5

I currently spend one day in this issue,still failed to download a file from an url in angular 5

leadGenSubmit() {

    return this.http.get('http://kmmc.in/wp-content/uploads/2014/01/lesson2.pdf',
    {responseType:ResponseContentType.Blob}).subscribe((data)=>{
        console.log(data);
        var blob = new Blob([data], {type: 'application/pdf'});
        console.log(blob);
        saveAs(blob, "testData.pdf");
    },
    err=>{
        console.log(err);
        }
    )
}

when I run above code it shows following error

ERROR TypeError: req.responseType.toLowerCase is not a function
    at Observable.eval [as _subscribe] (http.js:2187)
    at Observable._trySubscribe (Observable.js:172)

how can I solve this issue.Can any one post the correct code to download a pdf file from an url in angular 5?

like image 788
Pranab V V Avatar asked Apr 26 '18 09:04

Pranab V V


People also ask

How do I make a PDF downloadable link in HTML?

With the use of the <a> tag download attribute, we can download pdf files, images, word files, etc. The download attribute specifies that the target (the file specified in the href attribute) will be downloaded when a user clicks on the hyperlink.


1 Answers

I think you should define header and responseType like this:

let headers = new HttpHeaders();
headers = headers.set('Accept', 'application/pdf');
return this.http.get(url, { headers: headers, responseType: 'blob' });
like image 123
ochs.tobi Avatar answered Sep 18 '22 12:09

ochs.tobi