简体   繁体   English

如何在AngularJS中显示base64图像

[英]How to display base64 image in AngularJS

Need help again ;) 再次需要帮助;)

I would like to show a photo in my Angular page. 我想在“ Angular”页面中显示照片。 These are my steps, 这些是我的步骤

  1. REST API gets a document/record from backend MongoDB. REST API从后端MongoDB获取文档/记录。 The base64 images are stored as this. base64图像将以此存储。

在此处输入图片说明

  1. The images/data are loaded into an array {{file_src[i]}} in the component code, then the component HTML will show the image as below 将图像/数据加载到组件代码中的数组{{file_src [i]}}}中,然后组件HTML将如下所示显示图像

在此处输入图片说明

Situations: 情况:

  1. If I used "img srcs={{file_src[i]}}", I got insecure operation. 如果使用“ img srcs = {{file_src [i]}}”,则操作不安全。 My REST API server has CORS enabled. 我的REST API服务器已启用CORS。 Since the image is base64 data and doesn't have any URL, I don't know it is related to CORS. 由于该图像是base64数据,并且没有任何URL,因此我不知道它与CORS有关。

在此处输入图片说明

  1. I googled around and found the ng-src and data-ng-src directives. 我四处搜寻,发现了ng-src和data-ng-src指令。 Both of them don't work too. 他们两个都不工作。 Please see my binding error below. 请在下面查看我的绑定错误。

在此处输入图片说明

Question: how to show the base64 image in my Angular page? 问题:如何在我的Angular页面中显示base64图像?

------Code as requested by Vic-------- ------维克要求的代码--------

<section class="fhirForm">
        <fieldset>
            <legend class="hd">
                <span class="text">Photo</span>
            </legend>
            <div class="bd" formArrayName="photos">
                <div *ngFor="let photo of patFormGroup.controls.photos.controls; let i=index" class="panel panel-default">
                    <div class="panel-body" [formGroupName]="i">

                        <label>Description</label>
                        <input type="text" class="form-control" formControlName="desc">

                        <label>Photo</label>
                        <input type="file" size="30" multiple formControlName="photo" name="crud8" (change)="photoChange(input, i)" #input>
                        <!-- img src={{file_srcs[i]}} crossorigin="anonymous" alt="" /-->
                        <img data-ng-src={{file_srcs[i]}} alt="" />

                        <span class="glyphicon glyphicon-remove pull-right" *ngIf="patFormGroup.controls.photos.controls.length > 1" (click)="removePhoto(i)"></span>
                    </div>
                </div>
            </div>
        </fieldset>

        <div class="margin-20">
            <a (click)="addPhoto()" style="cursor: default">
                <small>Add another photo +</small>
            </a>
        </div>
    </section>

  initPhoto(desc?: String, photo?: string) {
    //Add new entry on the 1 dimensional array. Allow 1 photo per section
    this.file_srcs.push(photo);
    console.log("Photo for file_srcs: [" + this.file_srcs[this.file_srcs.length - 1] + "]");

    return this.formBuilder.group({
      desc: [desc],
      photo: [photo]
    });
  }

Please see the console.log. 请参阅console.log。 It showed that this.file_srcs are valid. 它表明this.file_srcs是有效的。

在此处输入图片说明

------------- Error Message in Chrome ------- ------------- Chrome中的错误消息-------

在此处输入图片说明

------------- UPDATE 1 ----------- -------------更新1 -----------

If I commented out the "input type=file ..." line above the "img src" as below, I can see the photo. 如果我将“ img src”上方的“ input type = file ...”行注释掉,则可以看到照片。 What's wrong with my input? 我的输入有什么问题? Sorry, I don't remember what is that #input for. 抱歉,我不记得#input的作用是什么。

Hence, my issue may not be in the photo, but on the input line ;) Shame on me!!! 因此,我的问题可能不在照片中,而在输入行中;)对我感到羞耻!!!

                <label>Photo</label>
                <!-- input type="file" size="30" formControlName="photo" name="crud8" (change)="photoChange(input, i)" #input -->
                <img src={{file_srcs[i]}} crossorigin="anonymous" alt="" />

在此处输入图片说明

--------- RESOLVED ----------- - - - - - 解决 - - - - - -

Thanks a lot for all the help!!! 非常感谢所有帮助!!!

i. 一世。 the base64 image isn't the root cause; base64映像不是根本原因;

ii. II。 the file input "input type=file" was initialized by incorrect supplying the base64 image as the default value. 文件输入“输入类型=文件”是通过不正确提供base64映像作为默认值来初始化的。 It caused the error - failed to set the value of HtmlInputElement is correct in IE. 它导致了错误-无法在IE中正确设置HtmlInputElement的值。 The error message 'Insecure Operation' may be misleading in Firefox. 错误消息“不安全操作”在Firefox中可能会引起误解。

Hence, the root cause is not related to the base64 image. 因此,根本原因与base64映像无关。 I would like to delete this thread a week later. 我想在一周后删除此线程。

initPhoto(desc?: String, photo?: string) {

this.file_srcs.push(photo);
console.log("Photo for file_srcs[" + (this.file_srcs.length - 1) + "]: [" + this.file_srcs[this.file_srcs.length - 1] + "]");

return this.formBuilder.group({
  desc: [desc],
  photo: [""]     //This was photo: [photo]. After supplying the default value as "", it works well.
});

Best regards, 最好的祝福,

Autorun 自动运行

Fetch the base64 content in your controller like this: 像这样在控制器中获取base64内容:

$http.get($scope.user.photo).then(function(response) {
    $scope.user.data = response.data;
});

then display it on view 然后在视图上显示

<img data-ng-src="data:image/png;base64,{{user.data}}"/>

I use base64 image a lot and haven't see that error before. 我经常使用base64映像,以前没有看到该错误。 Is it caused by the crossorigin attribute? 是由crossorigin属性引起的吗?

 angular.module('test', []).controller('Test', Test); function Test($scope) { $scope.base64 = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA8AAAAOCAYAAADwikbvAAAA+0lEQVQ4T6WS3W3CMBSFz40QvDJCu0GYALJB2QBeUFzjCm9AJ0gLMQl9STegG5QNYARG6CsI+SKjpmppSY3w8/10fnwIVzy6lE2SollrbBcAPV8ET2fzOzAXDNYPUrx6wxOT9QjkwL4DnWMvODV5wUAP4EclxbiM+i88meUJMUYA3pSMu987qoRLqwDW+10j0rr/4QV/lrNwxwGClpSD9enPHJXTdD5i4vY+YK2F2BjzElrYdwDN05x/KpelMOGJGB0AIQGboYxvz23hR+apyVcO+jq2HCklll7wcT31rbMbgrBU93FUtcBfbSdZdlOztILlbpWq90jOqR8Au8VfIQFLZecAAAAASUVORK5CYII="; } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script> <div ng-app='test' ng-controller='Test'> <img src={{base64}} /> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM