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

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

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 **

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

It's Angular side template code

<div class="container-fluid">
  <div class="row">
    <div *ngFor="let adv of advs">
      <div class="col-12 col-sm-6 col-lg-3 col-xl-3">
        <a routerLink="details">
          <div class="card ">
            <div *ngFor="let img of adv.images">
              <img src="img.Base64Image" class="card-img-top" alt="...">
            </div>
            <div class="card-body">
              <div class="heading">
                <h4 class="text-center">{{adv.name}}</h4>
              </div>
              <p class="card-text">
                {{adv.description}}
              </p>
            </div>
          </div>
        </a>
      </div>
    </div>
  </div>
</div>

Component code

export class UserHomeComponent implements OnInit {
    advs: Advertisement[] = [];

  constructor(private advertisementService: AdvertisementsService) { }

  ngOnInit(): void {
    this.advertisementService.getAdvertisements().subscribe(data=>{
      for(let adv of data){
        for(let img of adv.images){
          console.log(img);
        }
      }
      this.advs = data;
    });
  }
}

请注意,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 **

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

It's Angular side template code

&lt;div class=&quot;container-fluid&quot;&gt;
  &lt;div class=&quot;row&quot;&gt;
    &lt;div *ngFor=&quot;let adv of advs&quot;&gt;
      &lt;div class=&quot;col-12 col-sm-6 col-lg-3 col-xl-3&quot;&gt;
        &lt;a routerLink=&quot;details&quot;&gt;
          &lt;div class=&quot;card &quot;&gt;
            &lt;div *ngFor=&quot;let img of adv.images&quot;&gt;
              &lt;img src=&quot;img.Base64Image&quot; class=&quot;card-img-top&quot; alt=&quot;...&quot;&gt;
            &lt;/div&gt;
            &lt;div class=&quot;card-body&quot;&gt;
              &lt;div class=&quot;heading&quot;&gt;
                &lt;h4 class=&quot;text-center&quot;&gt;{{adv.name}}&lt;/h4&gt;
              &lt;/div&gt;
              &lt;p class=&quot;card-text&quot;&gt;
                {{adv.description}}
              &lt;/p&gt;
            &lt;/div&gt;
          &lt;/div&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;/div&gt;

Component code

export class UserHomeComponent implements OnInit {
    advs: Advertisement[] = [];

  constructor(private advertisementService: AdvertisementsService) { }

  ngOnInit(): void {
    this.advertisementService.getAdvertisements().subscribe(data=&gt;{
      for(let adv of data){
        for(let img of adv.images){
          console.log(img);
        }
      }
      this.advs = data;
    });
  }

答案1

得分: 0

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

最后,src的值应为:

data:image/png;base64,{base64string}

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

&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也使用驼峰命名法。

export interface Image {
  ...
  base64Image: string;
}

在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:

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.

&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.

export interface Image {
  ...
  base64Image: string;
}

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:

确定