import { Directive , EventEmitter , Input , Output , SimpleChanges , } from '@angular/core' ; import { DomSanitizer } from '@angular/platform-browser' ; @ Directive ( { selector : '[hsDownload]' , } ) export class HsDownloadDirective { @ Input ( ) hsDownload = '' ; @ Input ( ) mimeType = '' ; @ Output ( ) downloadPrepared = new EventEmitter < string > ( ) ; exportedHref : any ; constructor ( private DomSanitizer ) { } ngOnChanges ( changes ) { const data = new Blob ( [ this . hsDownload ] , { type : this . mimeType } ) ; const url = URL . createObjectURL ( data ) ; if ( this . exportedHref ) { URL . revokeObjectURL ( this . exportedHref ) ; } this . exportedHref = this . DomSanitizer . bypassSecurityTrustResourceUrl ( url ) ; setTimeout ( ( ) => { this . downloadPrepared . emit ( this . exportedHref ) ; } , 0 ) ; } }