【问题标题】:Angular with Firestore storage getting image from bucket使用 Firestore 存储从存储桶获取图像的 Angular
【发布时间】:2021-03-02 21:55:40
【问题描述】:

我在存储桶中保存了一些图像。我正在尝试根据 AgencyId 在我的模板中加载这些图像。现在,我正在获取图像,但前提是我放置了路径。如何获取我的 AgencyId - 这是我的图像的名称到我的组件中,然后添加图像 etx,或者我将如何更改 myu 组件中的代码,然后如何在我的模板中获取它。

我的组件

import {ChangeDetectionStrategy, Component, OnInit} from '@angular/core';
import {tap} from 'rxjs/operators';
import {AngularFirestore} from '@angular/fire/firestore';
import {Agency} from './Agency.interface';
import {AngularFireStorage, AngularFireStorageReference, AngularFireUploadTask} from '@angular/fire/storage';

@Component({
  selector: 'app-agencies',
  templateUrl: './agencies.component.html',
  styleUrls: ['./agencies.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush

})
export class AgenciesComponent implements OnInit {
  ref: AngularFireStorageReference;
  logoUrl: any;
  bcUrl: any;
  filePath: string;


  agencies$ = this.afs.collection<Agency[] | any>('agencies').valueChanges().pipe(tap(console.log));

  constructor(public afs: AngularFirestore, private afStorage: AngularFireStorage) {
    const storage = this.afStorage.storage.app.storage(
   'gs://agency-logos-paid-keeper');
    this.logoUrl = storage.ref().child(`/thumbnails/12345678910_700x100.png`)
      .getDownloadURL();


  }

我的模板

<div class="clr-row" *ngIf="agencies$ | async as agencies">
  <div class="clr-col-lg-4 clr-col-12">
    <div class="card" *ngFor="let agency of agencies">
      <div class="card-header">
        <div><b>{{agency.name}}</b></div>
        <div>{{agency.phone |slice:0:3 }}-{{ agency.phone
          | slice:3:6 }}-{{ agency.phone | slice:6:10 }}</div>

      </div>
      <div class="card-block">
        <div class="card-media-block">
          <div class="card-media-description">
            <img [src]="logoUrl | async" class="card-media-image" alt="AFS" id="logoImg"/>
          </div>
        </div>
        <div class="card-text">

<!--          <div class="card-media-block">-->
<!--            <div class="card-media-description">-->
              <img src="/assets/card.png">
          Time Sheet:
          <span *ngIf="agency.pocTimesheetRequired == true"><cds-icon shape="success-standard"
                                                                      solid></cds-icon></span>
          <span *ngIf="agency.pocTimesheetRequired == false"><cds-icon shape="times" solid></cds-icon></span>
<!--            </div>-->
<!--          </div>-->
          <!--            {{agency.pocTimesheetRequired}}-->
<!--          (click)="uploadImage($event, agency.agencyId)"-->
        </div>
      </div>
      <div class="card-footer">
        <button class="btn btn-sm btn-primary"
                routerLink="/agencies/edit-agencies/{{agency.agencyId}}"
        >Edit Agency
        </button>
        <button class="btn btn-sm btn-warn"

        >Add Agency
        </button>
      </div>
    </div>
</div>
</div>

【问题讨论】:

    标签: angular firebase image google-cloud-firestore storage


    【解决方案1】:

    解决方法不是从存储桶中获取图片,而是将存储桶中保存的文件的url存储到Firestore中。

    const ref = this.storage.ref('/path/to/file.ext')  // Filepath 
    
    // 'file' comes from the Blob or File API
    ref.put(file).then((snapshot) => {
      snapshot.ref.getDownloadURL().then(
        url => {
          // Save the url alongwith agency data in the same collection 
        }
      )
    });
    

    现在,您将始终拥有文件的 url 以及其他数据。当您以后想要从存储中删除或更新文件时,这也很有用。

    【讨论】:

    • 感谢@T 提供的有用信息。苏尼尔·饶。两个问题:当您说文件来自 Blob 或 File Api 时,您能更具体一点吗?问题二:我不知道我的孩子路径应该是什么。它是集合的名称吗?如果我通过存储桶路径,像这样 gs://agency-logos-paid-keeper/thumbnails.png 我收到错误 Firebase Storage: ref() expected a child path but got a URL, use refFromURL instead.跨度>
    • Filepath 是您希望在存储桶中看到的路径。例如'/uploads/agency/id/thumbnail.png'
    • 文件就是你要上传的文件。它使用文件 API。 developer.mozilla.org/en-US/docs/Web/API/File 可以像 let file= new File([""], "foo.bar") 一样使用它。另请参阅firebase.google.com/docs/storage/web/upload-files
    猜你喜欢
    • 2021-10-13
    • 2018-03-20
    • 1970-01-01
    • 1970-01-01
    • 2021-02-27
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多