Ionic file download from php server using http get

I was searching a lot on this and today I got success. So I am sharing my code here.

Note:- Any suggestion are welcome if someone wants any change or have better solution, please post here.

Home.ts file:-

import { Component } from '@angular/core';

import { HttpClient } from '@angular/common/http';

@Component({

  selector: 'app-home',

  templateUrl: 'home.page.html',

  styleUrls: ['home.page.scss'],

})

export class HomePage {

  constructor(

    private http: HttpClient

  ) { }

  downloadFile(route: string, filename: string = null): void {

    this.http.get(route, { responseType: 'blob' }).subscribe(

      (response: any) => {

        let dataType = response.type;

        let binaryData = [];

        binaryData.push(response);

        let downloadLink = document.createElement('a');

        downloadLink.href = window.URL.createObjectURL(new Blob(binaryData, { type: dataType }));

        if (filename) {

          downloadLink.setAttribute('download', filename);

        }

        document.body.appendChild(downloadLink);

        downloadLink.click();

      }

    )

  }

}