Austin Muts

Austin Muts

  • 1.3k
  • 306
  • 119.1k

Reusable Material dialogue dialogue not opening external url with pdf

Aug 10 2020 9:43 AM
I have a material dialog template that i want to use to open and download pdf files from an external link(url) in another component but its not working.It says the url is unsafe.Any idea will be appreciated
  1. import { Component, OnInit, ViewChild, ElementRef, Inject, forwardRef, ChangeDetectorRef, SecurityContext,Pipe, PipeTransform,Input,AfterViewInit } from '@angular/core';  
  2. import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser';  
  3. //import { DialogComponent, DialogService } from 'ng2-bootstrap-modal';  
  4. import { ElementAst } from '@angular/compiler';  
  5. import {ConfirmdialogService} from './../services/confirmdialog.service';  
  6. import {MAT_DIALOG_DATA,MatDialogRef,MatDialog} from '@angular/material/dialog';  
  7. import {EmployeedocsComponent} from './../upload/employeedocs/employeedocs.component';  
  8. import { DocumentService } from "../services/document.service";  
  9. import { ShareReplayConfig } from 'rxjs/internal-compatibility';  
  10. export interface ViewerModel {  
  11. title: string;  
  12. NodeID: string;  
  13. }  
  14. @Component({  
  15. selector: 'viewer',  
  16. template: `  
  17. <h1 mat-dialog-title>{{title}}</h1>  
  18. <mat-dialog-content>  
  19. <iframe width="640" height="390" [src]="NodeID | safe">  
  20. </iframe>  
  21. </mat-dialog-content>  
  22. <div mat-dialog-actions>  
  23. <button mat-button (click)="onNoClick()">Close</button>  
  24. </div>  
  25. `  
  26. })  
  27. @Pipe({ name: 'safe' })  
  28. export class ViewerComponent implements PipeTransform {  
  29. constructor(  
  30. public dialogRef: MatDialogRef<ViewerComponent>,public sanitizer: DomSanitizer,  
  31. @Inject(MAT_DIALOG_DATA) public data: ViewerModel) { }  
  32. title: string;  
  33. NodeID: string;  
  34. transform(NodeID) {  
  35. return this.sanitizer.bypassSecurityTrustResourceUrl(this.NodeID);  
  36. }  
  37. ngOnInit(){  
  38. }  
  39. onNoClick(): void {  
  40. this.dialogRef.close();  
  41. }  
  42. }  
Here is how i want to open pdf Urls from another component using the above materail dialog template
  1. this.url = this.sanitizer.bypassSecurityTrustResourceUrl(this.DownloadURL.concat(employid) +"/" +this.downloaddoc.Name);  
  2. const dialogWithForm = this.dialogModel.open(ViewerComponent, {  
  3. data: { title: this.downloaddoc.Name, NodeID: this.url}  
  4. });  
  5. dialogWithForm.afterClosed().subscribe(result => {  
  6. console.log('You have closed the dialog');  
  7. if (result) {  
  8. this.downloaddoc.Name = result.title;  
  9. this.url = result.NodeID;  
  10. }  
  11. });