Angular 6 - Get download URL of Firebase Storage file after upload


Rosenberg

The task I am trying to accomplish here is simple.

  1. Upload file to Firebase Storage

  2. Grab the link to the file and insert it into the form.

The problem is, I can't get the download URL.

When I upload the content, it does upload, but I get the following error message:

Object { code_: "storage/object-not-found", message_: "Firebase Storage: Object 'rnmgm3vvpz' does not exist.", serverResponse_: "{\n  \"error\": {\n    \"code\": 404,\n    \"message\": \"Not Found.  Could not get object\"\n  }\n}", name_: "FirebaseError" }

Here is the code to upload on component.ts:

upload(event) {
  const id = Math.random().toString(36).substring(2);
  this.ref = this.afStorage.ref(id);
  this.task = this.ref.put(event.target.files[0]);
  this.uploadState = this.task.snapshotChanges().pipe(map(s => s.state));
  this.uploadProgress = this.task.percentageChanges();
  this.downloadURL = this.ref.getDownloadURL();
}

and on component.html:

<input type="file" (change)="upload($event)" accept=".png,.jpg" />

How to get downloadURL after file upload?

dAxx_

You should add finalize() in your pipeline like:

this.task.snapshotChanges().pipe(
  finalize(() => {
    this.downloadURL = this.ref.getDownloadURL(); // <-- Here the downloadURL is available.
  })
).subscribe();

In the finalize() step, the downloadURL is available, so you can get it from the ref asynchronously. --UPDATE
You said you're using Angular 6, so I'm assuming you're using the latest version of Firebase.
They changed the Task's getDownloadURL() to an Observable, so to get the actual URL you just need to subscribe.

this.task.snapshotChanges().pipe(
  finalize(() => {
    this.ref.getDownloadURL().subscribe(url => {
      console.log(url); // <-- do what ever you want with the url..
    });
  })
).subscribe();

Related


Get download URL from Firebase Storage in Angular

Monther I'm trying to get the download URL after uploading a file to Firebase Storage, so far I've been able to upload the file, but for the URL, I've been getting undefined, this is my code: onFileChanged(event) { const file: File = event.target.files[0];

Get download URL from Firebase Storage in Angular

Monther I'm trying to get the download URL after uploading a file to Firebase Storage, so far I've been able to upload the file, but for the URL, I've been getting undefined, this is my code: onFileChanged(event) { const file: File = event.target.files[0];

Get download URL of Firebase Storage file

birdcage I have a mobile app that requires Firebase registration to backup/restore custom user data in Firebase. I would like to know if an authorized user can somehow get the url of his own stored file and download it to his computer. Frank Vampfelen If a use

Unable to download URL using Firebase Storage after upload

Ephraim Schmitt I am using Angular and AngularFire2. I'm trying to upload an image to Firebase storage, and once that's done, I can get that reference and get the download URL and upload it to the database. For some reason, even though the upload is complete a

Unable to download URL using Firebase Storage after upload

Ephraim Schmitt I am using Angular and AngularFire2. I'm trying to upload an image to Firebase Storage, and once that's done, I can get that reference and get the download URL and upload it to the database. For some reason, even though the upload is complete a

Can't get Firebase image URL from storage after upload

ly I'm using Firebase Storage for an app so that I can store images to be displayed in a popup. Now I can reference the image, but can't get URLthe image using getDownloadURL(). I've tried everything but I can't seem to get the URL, it just shows an error in t

Can't get Firebase image URL from storage after upload

ly I'm using Firebase Storage for an app so that I can store images to be displayed in a popup. Now I can reference the image, but can't get URLthe image using getDownloadURL(). I've tried everything but I can't seem to get the URL, it just shows an error in t

Can't get Firebase image URL from storage after upload

ly I'm using Firebase Storage for an app so that I can store images to be displayed in a popup. Now I can reference the image, but can't get URLthe image using getDownloadURL(). I've tried everything but I can't seem to get the URL, it just shows an error in t

Can't get Firebase image URL from storage after upload

ly I'm using Firebase Storage for an app so that I can store images to be displayed in a popup. Now I can reference the image, but can't get URLthe image using getDownloadURL(). I've tried everything but I can't seem to get the URL, it just shows an error in t

Can't get Firebase image URL from storage after upload

ly I'm using Firebase Storage for an app so that I can store images to be displayed in a popup. Now I can reference the image, but can't get URLthe image using getDownloadURL(). I've tried everything but I can't seem to get the URL, it just shows an error in t

Can't get Firebase image URL from storage after upload

ly I'm using Firebase Storage for an app so that I can store images to be displayed in a popup. Now I can reference the image, but can't get URLthe image using getDownloadURL(). I've tried everything but I can't seem to get the URL, it just shows an error in t

Can't get Firebase image URL from storage after upload

ly I'm using Firebase Storage for an app so that I can store images to be displayed in a popup. Now I can reference the image, but can't get URLthe image using getDownloadURL(). I've tried everything but I can't seem to get the URL, it just shows an error in t

Can't get Firebase image URL from storage after upload

ly I'm using Firebase Storage for an app so that I can store images to be displayed in a popup. Now I can reference the image, but can't get URLthe image using getDownloadURL(). I've tried everything but I can't seem to get the URL, it just shows an error in t

Can't get Firebase image URL from storage after upload

ly I'm using Firebase Storage for an app so that I can store images to be displayed in a popup. Now I can reference the image, but can't get URLthe image using getDownloadURL(). I've tried everything but I can't seem to get the URL, it just shows an error in t

Can't get Firebase image URL from storage after upload

ly I'm using Firebase Storage for an app so that I can store images to be displayed in a popup. Now I can reference the image, but can't get URLthe image using getDownloadURL(). I've tried everything but I can't seem to get the URL, it just shows an error in t

Can't get Firebase image URL from storage after upload

ly I'm using Firebase Storage for an app so that I can store images to be displayed in a popup. Now I can reference the image, but can't get URLthe image using getDownloadURL(). I've tried everything but I can't seem to get the URL, it just shows an error in t

Can't get Firebase image URL from storage after upload

ly I'm using Firebase Storage for an app so that I can store images to be displayed in a popup. Now I can reference the image, but can't get URLthe image using getDownloadURL(). I've tried everything but I can't seem to get the URL, it just shows an error in t