Quantcast
Channel: Ionic Framework - Ionic Forum
Viewing all articles
Browse latest Browse all 48980

Ionic file download from php server using http get

$
0
0

I was searching a lot on this and today I got success. So I am sharing my code 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();

      }

    )

  }

}

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 48980

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>