Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

206
Vistas
Firebase storage percentChanges() method returns NaN

I am trying to build a firebase storage service from an angular client, which will upload a user profile image and return uploadProgress$ and DdownloadUrl$ observables.

uploadProgress$ is a Observable which will store the percentChanges() value and downloadUrl$ is a Observable which will contain the getDownloadURL() value.

When binding the uploadProgress$ to angular using the [value] = "uploadProgress$ | async" pipe it returns NaN.

I am able to receive the image link when the upload has finished.

My code is as follows:

firebase-storage.service

import { Injectable } from '@angular/core';
import {
  AngularFireStorage,
  AngularFireUploadTask,
} from '@angular/fire/compat/storage';
import { finalize, from, Observable, switchMap } from 'rxjs';
import { imageFile } from '../models/file-upload';

interface FilesUploadMetadata {
  downloadUrl$: Observable<string>;
  uploadProgress$: Observable<number | undefined>;
}

@Injectable({
  providedIn: 'root',
})
export class FirebaseStorageService {
  constructor(private fireStorage: AngularFireStorage) {}

  uploadFileAndGetMetadata(
    fileToUpload: imageFile,
    mediaFolderPath: string,
    userName: string
  ): FilesUploadMetadata {
    const filePath = `${mediaFolderPath}/${userName}_profileImage`;
    const storageRef = this.fireStorage.ref(filePath);
    const uploadTask: AngularFireUploadTask = this.fireStorage.upload(filePath, fileToUpload);

    uploadTask
      .snapshotChanges()
      .pipe(
        finalize(() => {
          storageRef.getDownloadURL().subscribe((downloadURL) => {
            fileToUpload.url = downloadURL;
            fileToUpload.name = fileToUpload.file.name;
          });
        })
      )
      .subscribe();
          
    return {
      uploadProgress$: uploadTask.percentageChanges(),
      downloadUrl$: storageRef.getDownloadURL(),
    };
  }
}

user-profile.component.ts

upload() {
    this.submitted = true;
    const file: File = this.selectedFile.item(0) as File;
    this.currentFileUpload = new imageFile(file);
    const userName = this.currentUser.username;
    const mediaFolderPath = `Profile/${this.currentUser.email}/profileImage/`;
    const { uploadProgress$, downloadUrl$ } =
      this.firebaseService.uploadFileAndGetMetadata(
        this.currentFileUpload,
        mediaFolderPath,
        userName
      );
    uploadProgress$.subscribe((percent) => {
      console.log(typeof percent);
      console.log(percent);
    });
    downloadUrl$.subscribe((downloadUrl) => {
      console.log(downloadUrl);
    });
  }
}

user-profile.component.html

<div *ngIf="submitted">
<mat-progress-bar [value]="uploadProgress$ | async" mode="determinate">
</mat-progress-bar>
</div>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

const { uploadProgress$, downloadUrl$ } is local. It's only available inside the function. You must create it outside as public.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda