How i can display image or images in angular template which is received from Asp.Net Rest Api as a base64 string

huangapple go评论95阅读模式
英文:

How i can display image or images in angular template which is received from Asp.Net Rest Api as a base64 string

问题

以下是翻译好的部分:

"I'm posting advertisement by using angular v14 and asp.net rest api, the posted advertisement is in db by using byte[] property in rest api as a binary data, i want to display that posted advertisement on home page of my project and i received & display all other things like name, description but i can't display image while i received it as base64string from rest api like that "/9j/4QKKRXhpZgAASUkqAAgAAAAIAA8BAgAHAAAAbgAAABABAgAWAAAAdgAAA...." & i received it in angular same as above.

**Here in .net rest api i'm converting it into base64string & sending it to angular template **

  1. public static ImageModel ToModel(this AdvertisementImage entity)
  2. {
  3. return new ImageModel { Id = entity.Id, Base64Image = Convert.ToBase64String(entity.Contents) };
  4. }

It's Angular side template code

  1. <div class="container-fluid">
  2. <div class="row">
  3. <div *ngFor="let adv of advs">
  4. <div class="col-12 col-sm-6 col-lg-3 col-xl-3">
  5. <a routerLink="details">
  6. <div class="card ">
  7. <div *ngFor="let img of adv.images">
  8. <img src="img.Base64Image" class="card-img-top" alt="...">
  9. </div>
  10. <div class="card-body">
  11. <div class="heading">
  12. <h4 class="text-center">{{adv.name}}</h4>
  13. </div>
  14. <p class="card-text">
  15. {{adv.description}}
  16. </p>
  17. </div>
  18. </div>
  19. </a>
  20. </div>
  21. </div>
  22. </div>
  23. </div>

Component code

  1. export class UserHomeComponent implements OnInit {
  2. advs: Advertisement[] = [];
  3. constructor(private advertisementService: AdvertisementsService) { }
  4. ngOnInit(): void {
  5. this.advertisementService.getAdvertisements().subscribe(data=>{
  6. for(let adv of data){
  7. for(let img of adv.images){
  8. console.log(img);
  9. }
  10. }
  11. this.advs = data;
  12. });
  13. }
  14. }

请注意,Angular 模板中的图片标签的src属性应该使用双括号包裹,像这样:<img [src]="img.Base64Image" class="card-img-top" alt="...">,以正确绑定数据。

英文:

I'm posting advertisement by using angular v14 and asp.net rest api,the posted advertisement is in db by using byte[] property in rest api as a binary data,i want to display that posted advertisement on home page of my project and i received & display all other things like name,description but i can't display image while i received it as base64string from rest api like that "/9j/4QKKRXhpZgAASUkqAAgAAAAIAA8BAgAHAAAAbgAAABABAgAWAAAAdgAAA...." & i received it in angular same as above.
**Here in .net rest api i'm converting it into base64string & sending it to angular template **

  1. public static ImageModel ToModel(this AdvertisementImage entity)
  2. {
  3. return new ImageModel { Id = entity.Id, Base64Image = Convert.ToBase64String(entity.Contents) };
  4. }

It's Angular side template code

  1. &lt;div class=&quot;container-fluid&quot;&gt;
  2. &lt;div class=&quot;row&quot;&gt;
  3. &lt;div *ngFor=&quot;let adv of advs&quot;&gt;
  4. &lt;div class=&quot;col-12 col-sm-6 col-lg-3 col-xl-3&quot;&gt;
  5. &lt;a routerLink=&quot;details&quot;&gt;
  6. &lt;div class=&quot;card &quot;&gt;
  7. &lt;div *ngFor=&quot;let img of adv.images&quot;&gt;
  8. &lt;img src=&quot;img.Base64Image&quot; class=&quot;card-img-top&quot; alt=&quot;...&quot;&gt;
  9. &lt;/div&gt;
  10. &lt;div class=&quot;card-body&quot;&gt;
  11. &lt;div class=&quot;heading&quot;&gt;
  12. &lt;h4 class=&quot;text-center&quot;&gt;{{adv.name}}&lt;/h4&gt;
  13. &lt;/div&gt;
  14. &lt;p class=&quot;card-text&quot;&gt;
  15. {{adv.description}}
  16. &lt;/p&gt;
  17. &lt;/div&gt;
  18. &lt;/div&gt;
  19. &lt;/a&gt;
  20. &lt;/div&gt;
  21. &lt;/div&gt;

Component code

  1. export class UserHomeComponent implements OnInit {
  2. advs: Advertisement[] = [];
  3. constructor(private advertisementService: AdvertisementsService) { }
  4. ngOnInit(): void {
  5. this.advertisementService.getAdvertisements().subscribe(data=&gt;{
  6. for(let adv of data){
  7. for(let img of adv.images){
  8. console.log(img);
  9. }
  10. }
  11. this.advs = data;
  12. });
  13. }

答案1

得分: 0

要显示base64图像,应提供以以下前缀开头的base64字符串:“data:{content/type};base64,”如文章所述,其中{content/type}是资源的内容类型。

最后,src的值应为:

  1. data:image/png;base64,{base64string}

请注意,[src]会以方括号应用,因为我们希望将该值作为变量传递,而不是作为字符串化的值。

  1. &lt;img [src]=&quot;&#39;data:image/image/png;base64,&#39; + img.base64Image&quot; class=&quot;card-img-top&quot; alt=&quot;...&quot;&gt;

请注意,属性名称区分大小写。如果您的JSON响应以驼峰命名法返回,请确保您的Base64Image也使用驼峰命名法。

  1. export interface Image {
  2. ...
  3. base64Image: string;
  4. }

在StackBlitz上查看演示

英文:

To display the base64 image, you should provide the base64 string with the prefix: "data:{content/type};base64," as stated in the article, which {content/type} is the content type of the resource.

At the end, the value of src should be:

  1. data:image/png;base64,{base64string}

Do note that [src] is applied with a square bracket as we want to pass the value as a variable, instead of a stringified value.

  1. &lt;img [src]=&quot;&#39;data:image/image/png;base64,&#39; + img.base64Image&quot; class=&quot;card-img-top&quot; alt=&quot;...&quot;&gt;

Note that the property name is case-sensitive, if your JSON response returned is with camelCase, then your Base64Image should apply with camel case as well.

  1. export interface Image {
  2. ...
  3. base64Image: string;
  4. }

Demo @ StackBlitz

huangapple
  • 本文由 发表于 2023年5月30日 05:30:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76360417.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定