亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Angular 8 中將 base64 圖像放在墊卡上

如何在 Angular 8 中將 base64 圖像放在墊卡上

嚕嚕噠 2022-05-26 15:32:15
我正在 Spring Boot 和 mysql 中制作一個帶有后端的 Web 應用程序。我有一個帶有 Lob 屬性的模型,它以 base64 格式存儲圖像(來自前端)。現在我想帶上那個模型并將它的數據放在一張墊卡上。我得到了其他屬性,但不是圖像。這是我的 TSimport { ToyService } from 'src/app/services/toy-service';import { Toy } from 'src/app/services/models/toy';import { Router } from '@angular/router';@Component({  selector: 'app-toys',  templateUrl: './toys.component.html',  styleUrls: ['./toys.component.css']})export class ToysComponent implements OnInit {  toy = new Toy()  toys = [];  toysImages = [];  imageUrl: string;  image;  constructor(private toyService: ToyService, private router: Router) { }  ngOnInit() {    this.loadToys();  }  public loadToys() {    this.toyService.readPolicies().subscribe((toys: Toy[]) => {      this.toys = toys;            this.castingImages(toys)    })  }  //Here, i try to decode de base64 to a image  public castingImages(toys: Toy[]) {        for (let i = 0; i < toys.length; i++) {         this.image = new Image()        this.image.src = toys[i].picture;        toys[i].picture = this.image.src          }  }  redirect() {    this.router.navigate(['./toys']);  }}這里是我的 HTML    <ng-container *ngFor="let card of toys">        <mat-card class="example-card">            <mat-card-header>                <div mat-card-avatar class="example-header-image"></div>                <mat-card-title>{{card.id}}</mat-card-title>                <mat-card-subtitle>Dog Breed</mat-card-subtitle>            </mat-card-header>            <!-- And i want to do something like... -->            <img mat-card-image                src={{card.picture}}>            <mat-card-content>                <p>                    {{card.name}}                </p>            </mat-card-content>            <mat-card-actions>                <button mat-button>LIKE</button>                <button mat-button>SHARE</button>            </mat-card-actions>        </mat-card>    </ng-container></div><mat-divider></mat-divider>```Thank you so much.
查看完整描述

1 回答

?
斯蒂芬大帝

TA貢獻1827條經驗 獲得超8個贊

你可以試試以下


import { DomSanitizer } from '@angular/platform-browser';


export class ToysComponent implements OnInit {

  constructor(private toyService: ToyService, private router: Router, private sanitizer: DomSanitizer) { }


  public castingImages(toys: Toy[]) {    

    for (let i = 0; i < toys.length - 1; i++) { 

      toys[i].picture = this.sanitizer.bypassSecurityTrustResourceUrl('data:image/png;base64, ' + toys[i].picture);

    }

  }

}

在模板中,您可以調用該函數來清理 url


<img mat-card-image [src]="card.picture">


查看完整回答
反對 回復 2022-05-26
  • 1 回答
  • 0 關注
  • 134 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號