5

I want to download the .pdf file on button click I am using the below code

downloadMyFile() {
        const link = document.createElement('a');
        link.setAttribute('target', '_blank');
        link.setAttribute('href', 'http://designs.mydeievents.com/jq-3d-flip-book/books/pdf/aqua_imagica_visit_learnings.pdf');
        link.setAttribute('download', 'aqua_imagica_visit_learnings.pdf');
        document.body.appendChild(link);
        link.click();
        link.remove();
      }

<button (click)="downloadMyFile()">download File</button>

But when I click button it is redirected on a new page but I want to download 'aqua_imagica_visit_learnings.pdf' file on download. Kindly help me . here is my link [http://stackblitz.com/edit/angular-4hjad7]

Arjun Walmiki
  • 173
  • 1
  • 1
  • 13

4 Answers4

2

According to this .pdf file won't be downloaded if you use download attribute on anchor:

Some file types, however, (such as images, .pdf, .txt, and .doc for example) won’t be downloaded. They, instead, will be opened in the browser.

orlyohreally
  • 410
  • 6
  • 19
1

Use inspect element and make sure your link looks something like this. Instead of

link.setAttribute('download', 'aqua_imagica_visit_learnings.pdf');

try doing it this way.

link.download = "aqua_imagica_visit_learnings.pdf";
Sagar Khatri
  • 575
  • 3
  • 16
  • 2
    yes i try your solution but still, it is redirected on a new page. – Arjun Walmiki Sep 19 '19 at 06:48
  • can you try using any .img file to make sure that only some files like .PDFs have restriction in downloading? – Sagar Khatri Sep 19 '19 at 09:12
  • 1
    yes i also try with .img downloadMyFile() { const link = document.createElement('a'); link.setAttribute('target', '_blank'); link.setAttribute('href', 'http://designs.mydeievents.com/jq-3d-flip-book/books/image/2019-2020/Newlands_Year_Book/1.jpg'); link.download = '1.jpg'; document.body.appendChild(link); link.click(); link.remove(); } but it is redirect on another page. – Arjun Walmiki Sep 19 '19 at 09:46
  • Please create a stackblitz for this so we can figure out the root cause. – Sagar Khatri Sep 20 '19 at 06:34
1

You can download with ts:

 async download(id: number) {
        try {
            const res = await this.service.getDocument(id);
            this.downloadFile(res);
        } catch (e) {
            this.session.addSingleMessage(e.body.message);
        }
    }

    downloadFile(data) {
        const url = window.URL.createObjectURL(data);
        window.open(url);
    } 

template:

<button click="download(1)>Download</button>

service:

 async getDocument(id: number): Promise<any> {
        this.session.setLoading();
        const resp = await this.httpClient.get(`/api/document/${id}/download`, { responseType: 'blob' }).toPromise();
        this.session.resetLoading();
        return resp;
    }
tano
  • 2,657
  • 1
  • 15
  • 16
0

you can add this line on your html code,it works for me:

<a href="download/acme.pdf" download="Acme.pdf">Download pdf file</a>
Farouk Mhamdi
  • 311
  • 2
  • 8