Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to download file with blob function using angular 5

download any file type with blob function

private saveAsBlob(data: any) {
 const blob = new Blob([data._body],
 const file = new File([blob], 'image.png',
 FileSaver.saveAs(file);
}
like image 905
achref akrouti Avatar asked Sep 05 '18 10:09

achref akrouti


3 Answers

i got it like this it work with url:

download(row) {
    return this.Http
      .get(url, {
        responseType: ResponseContentType.Blob,
      })
      .map(res => {
        return {
          filename: row.name,
          data: res.blob()
        };
      })
      .subscribe(res => {
        let url = window.URL.createObjectURL(res.data);
        let a = document.createElement('a');
        document.body.appendChild(a);
        a.setAttribute('style', 'display: none');
        a.href = url;
        a.download = res.filename;
        a.click();
        window.URL.revokeObjectURL(url);
        a.remove();
      });
  }
like image 181
achref akrouti Avatar answered Oct 20 '22 06:10

achref akrouti


Try the following code

saveAsBlob(data: Response){
  var blob = new Blob([data._body], { type: 'image/png' });
  var url= window.URL.createObjectURL(blob);
  window.open(url);
}
like image 4
Sajeetharan Avatar answered Oct 20 '22 07:10

Sajeetharan


My solution to start a download. I used this in an NgRX Effect.

// ... getting blob form somewhere
const anchor = document.createElement('a');
anchor.download = "some_file_name.txt";
anchor.href = (window.webkitURL || window.URL).createObjectURL(blob);
anchor.click();
like image 1
Deitsch Avatar answered Oct 20 '22 07:10

Deitsch