Angular 5 – Firebase Storage with Angularfire 2

Hi Friends,

Hope you are well. In this post,  we are going to see how to use the newly added firebase storage support from the AngularFire2 library.

Let’s begin.

A screencast of this post

 

In this post we’ll see how to use Angularfire 2 library to store an image in firebase storage.

Scaffold out an application, and configure firebase in it. Since we have already discussed a lot on this in my previous posts, Let’s not go into details about this.

Now open up app.module.ts and add imports for everything. Your app.module.ts file would look like below.

(I have installed Material 2 as well. So I have added imports for those too.)

Open up app.component.html and add the below code.

This will add a button which upon clicking will open up file chooser dialog.

Open up app.component.ts and add the below code.

The above code does many things.

  1. The chooseFiles() function is the one that actually enables you to select a file.
  2. uploadpic() function is the one that uploads the file onto firebase storage.

    (Now, this might not work in your app since storing a file on firebase always requires authentication. I have disabled the rules and that’s why it works)

  3. The percentageChanges() will return an observable that will send you the value which you can then display on a progressBar.

    Once the file is uploaded it will be displayed in the image tag. (If you upload an image file).

This is a very simple example on how you could use angularfire2 to store files on firebase storage. In our forthcoming posts. We’ll see detailed use-cases of this in action

Hope this helped you guys. If you found this helpful, kindly share it with someone else and help them too.

Thanks for reading. Peace..:)

Liked it? Take a second to support admin on Patreon!