Download blob as a file in Angular

Recently I got a requirement to download base64 string as .pfx file in Angular application.

So I did that using Blob API in javascript.

More about Blob

So I did a sample application on stackblitz using Angular.

const data = 'some text';
const blob =new Blob([data],{type:'application/octet-stream'});
this.fileUrl =this.sanitizer.bypassSecurityTrustResourceUrl(window.URL.createObjectURL(blob));

As above we need to create Blob object. As second parameter we need to specify Mime type. And in Angular to make the Blob url safe in DOM we can use DomSanitizer.

We have to import DomSanitizer

import { DomSanitizer } from '@angular/platform-browser';</div>
// constructor
constructor(private sanitizer: DomSanitizer) { }

In html template


Working sample on stackblitz