使用angularfire2获取firebase中的图像网址

问题描述 投票:0回答:1

我正在使用angularFire2将图像添加到firebase存储中,我现在需要的是从图像中获取这些网址以在图库中显示它们以便它们可以在加载后显示,我尝试了一些但我没有成功,我希望你能帮助我,我的代码如下:

components.ts:

    import { Component, OnInit } from '@angular/core';
import { AngularFireStorage, AngularFireUploadTask } from 'angularfire2/storage';
import { Observable } from 'rxjs';
import { AngularFirestore } from 'angularfire2/firestore';
import { filter, switchMap } from '../../../../node_modules/rxjs/operators';
import { tap } from 'rxjs/operators';





@Component({
  selector: 'app-file-upload',
  templateUrl: './file-upload.component.html',
  styleUrls: ['./file-upload.component.scss']
})
export class FileUploadComponent implements OnInit {

  urlImages: string

  // Main task 
  task: AngularFireUploadTask;

  // Progress monitoring
  percentage: Observable<number>;

  snapshot: Observable<any>;

  // Download URL
  downloadURL: Observable<string>;

  // State for dropzone CSS toggling
  isHovering: boolean;
  //variable para obtener url
  image: string

  constructor(private storage: AngularFireStorage, private db: AngularFirestore) { }


  ngOnInit() {
  }



  toggleHover(event: boolean) {
    this.isHovering = event;
  }


  startUpload(event: FileList) {
    // The File object
    const file = event.item(0)

    // Client-side validation example
    if (file.type.split('/')[0] !== 'image') {
      console.error('unsupported file type :( ')
      return;
    }

    // El path del archivo en storage
    const path = `blog/${new Date().getTime()}_${file.name}`;

    // Metadata Personalizada
    const customMetadata = { app: 'CMS DsnAdmin' };

    // Declaramos la tarea
    this.task = this.storage.upload(path, file, { customMetadata })

    // Porcentaje de Carga
    this.percentage = this.task.percentageChanges();
    console.log('Image cargada!');
    this.snapshot = this.task.snapshotChanges()

    // Url del archivo
    const task = this.storage.upload(path, file);
    const ref = this.storage.ref(path);

  }

  // Determines if the upload task is active
  isActive(snapshot) {
    return snapshot.state === 'running' && snapshot.bytesTransferred < snapshot.totalBytes
  }

}

我希望你能帮助我,我是这个网络的新手,以防你以某种不正确的方式提出问题。

firebase angular6 firebase-storage
1个回答
0
投票

您可以设置已定义的属性以保存下载URL

downloadURL: Observable<string>;

通过在finalize()上使用task.snapshotChanges(),您可以从ref对象获取downloadURL。

// get notified when the download URL is available
task.snapshotChanges().pipe(
    finalize(() => this.downloadURL = ref.getDownloadURL() )
 )
.subscribe()

请访问:https://github.com/angular/angularfire2/blob/master/docs/storage/storage.md查看更多信息

© www.soinside.com 2019 - 2024. All rights reserved.