Ionic 2 + Angular 2: Images prepended with 'unsafe:' therefore not displaying even though they're fine

后端 未结 4 1211

For some reason, some of my images are being prepended with \'unsafe:\', which is causing them not to be rendered.

Q) Why is this happening an

相关标签:
4条回答
  • 2020-12-10 10:49

    In my case, I discovered, after have banged my head against the wall several times, that a litle and innocent whitespace in the end of the image url can make Ionic/Angular issue this error.

    0 讨论(0)
  • For anyone experiencing this issue, I have 'solved' it by using the following:

    Class:

    import {DomSanitizationService} from '@angular/platform-browser';
    
    constructor(private _DomSanitizationService: DomSanitizationService) {}
    

    Template:

    <img [src]="_DomSanitizationService.bypassSecurityTrustUrl(imgSrcProperty)"/>
    

    Where imgSrcProperty is the offending image base64 encoded.

    I still think this is a bug!

    0 讨论(0)
  • 2020-12-10 11:07

    in angular 5.2.6

    class:

    import { DomSanitizer } from '@angular/platform-browser';
    constructor(public _DomSanitizationService: DomSanitizer ) {}
    

    Template

    <img [src]="_DomSanitizationService.bypassSecurityTrustUrl(imgSrcProperty)"/>
    
    0 讨论(0)
  • 2020-12-10 11:08

    I would like to add an additional answer, so some of you will not have to debug for ages.

    We also came across this problem together with Ionic+Angular on iOS (WKWebView) and found out, that Base64 data urls are also considered "unsafe" once the Base64 string contains line breaks. Either MS Windows style CRLF or LF.

    We proceeded to remove those offending characters from base64 strings (an external interface was "pretty printing" them), which ultimately resolved the issue for us.

    Before applying the bypass mentioned by @Dave, I would check the string.

    0 讨论(0)
提交回复
热议问题