1

in my config.ts,

export const imageBase64 = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAiAAA...." 

in my conmonent.ts


import { DomSanitizer, SafeUrl } from '@angular/platform-browser';
import { imageBase64 } from './config'
...
  myImg: SafeUrl; 
  constructor(private sanitizer: DomSanitizer){
     this.myImg= this.sanitizer.bypassSecurityTrustUrl(imageBase64 );
  }

in my html

<img [src]="myImg" alt="" />

unfortunately, this fail, error message net::ERR_INVALID_URL

then i use bypassSecurityTrustResourceUrl fail too, What's wrong with that?

3
  • Please share imageBase64 value at jsoneditoronline.org. Commented Jan 6, 2020 at 4:41
  • 2
    Is imageBase64 is valid data? bcs your code is just working fine in jsfiddle stackblitz.com/edit/… with correct base 64 data Commented Jan 6, 2020 at 4:45
  • console.log(imageBase64)? before sanitizer? Commented Jan 6, 2020 at 5:29

4 Answers 4

2

We don't need to sanitize the base64 data. We can use string interpolation to show whatever the data we have in the form of string

<img src={{ imageBase64}} alt="" />
Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot, the base64 data was wrong lead to an error, and don't need to sanitize the base64 data.
0

You could use interpolation: src={{myImg}}

See below jsFiddle to help out:https://jsfiddle.net/Lt7aP/4/

Comments

0

Use get property to return url

public get myImg(): string {
  return this.sanitizer.bypassSecurityTrustUrl(imageBase64 );
}

Comments

0

Works fine. Try the demo and find where you went wrong.

<img [src]="imageBase64" alt="">

Demo : https://stackblitz.com/edit/angular-rzjusl

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.