Angular2 Base64清理不安全的URL值
发布时间:2020-12-17 07:53:57 所属栏目:安全 来源:网络整理
导读:来自我的服务器的响应如下: [{"coreGoalId":1,"title":"Core goal 1","infrastructure":"Sample Infrastructure","audience":"People","subGoals":null,"benefits":[{"benefitId":1,"what":"string","coreGoalId":1}],"effects":null,"steps":null,"images"
来自我的服务器的响应如下:
[{"coreGoalId":1,"title":"Core goal 1","infrastructure":"Sample Infrastructure","audience":"People","subGoals":null,"benefits":[{"benefitId":1,"what":"string","coreGoalId":1}],"effects":null,"steps":null,"images":[{"imagelId":1,"base64":"/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUnFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgonKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wgARCAIWAe4DASIAnAhEBAxEB/8QAHAABAAIDAQEB"}]}] 我正在尝试显示其中返回的base64图像. 在我的组件中: ngOnInit() { this.homeService.getGoals() .subscribe( goals => this.coreGoals = goals,error => this.errorMessage = <any>error); } 然后在模板中: <ul> <li *ngFor="let goal of coreGoals"> {{goal.title}} <img [src]="'data:image/jpg;base64,'+goal.images[0].base64 | safeHtml" /> </li> </ul> safeHtml是我创建的管道,如下所示: import { Pipe } from '@angular/core'; import { DomSanitizer } from '@angular/platform-browser'; @Pipe({name: 'safeHtml'}) export class SafeHtml { constructor(private sanitizer:DomSanitizer){} transform(html) { return this.sanitizer.bypassSecurityTrustHtml(html); } } 这给了我一个安全的URL,得到了一个HTML错误.这里出了什么问题?如果我从< img />中删除管道然后它说不安全的网址.
你需要
bypassSecurityTrustResourceUrl(html); 代替 bypassSecurityTrustHtml(html); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |