How to Upload Picture to Firebase Storage

Firebase storage or cloud storage for Firebase makes it super like shooting fish in a barrel to store your photos and videos. Firebase is Google platform known for scalability, cost effectiveness and security.

Information technology is reliable equally information technology based on Mobile Beginning approach. Which means it intelligently pauses and resumes the file transfer based on cyberspace connectivity.

If you are more of a video person that I have also attached the video at the end of the article.

For the terminal source code y'all tin can visit the Github repo

How often we see network failure when 90% file is uploaded and 10% left. We take to start the procedure all over again. Which not but wastes our time but bandwidth likewise. That'south where Firebase Storage shines.

Without farther ado let's gets cracking.

Setting up firebase project

I assume you lot already know how to prepare up a brand new firebase project. I wrote a dainty article about Setting up a project for firebase storage. The article is quite thorough simply I have linked to the verbal department.

Ane more than affair we need to do is to setup a binder for storage (which is too called in the server less technologies). From the menu click on the storage and and so click on the the large Get Started push button on the correct.

Firebase Storoage for Angular Getting Started page from technbuzz.com
Welcome screen telling about the firebase Storage and all its intricacies

Afterward clicking on getting started push, a firebase sets some rules and initialise a bucket that holds our storage. The initial rules are quite fragile in other words; quite insecure. A proper authentication and stricter rules are recommended for app in production.

Create a new binder, requite it a name which will be used as a reference subsequently. Although firebase storage holds every kind of files. I will name the folder images.

For the source code I volition be using firebase-storage branch of our GitHub repository. Firebase is already installed in this project with some dainty to do list. If y'all need a refresher just become through those steps.

Just clone the repo, add your environment file (that holds the firebase config secret strings) and spin upward the server.

What are we edifice

If was little hard for me to choose what kind of awarding. The most obvious 1 is to build the gallery. Which requires to create more view. For the sake of simplicity I went with the much easier one.

With the existing to practise list I added an option for the image. We can add together a todo without the prototype as well. The remainder of it is shown in following videos

[118 kb] Click the play button to load the video

Commencement with the Markup

The markup consists of two parts, listing and a from element to submit new todo. List uses ion-list with ion-item. We will introduce ion-thumbnail component to brandish the image if it exists. The ion-item becomes

We also need to modify the form element. Input of type file can be used to upload image to firebase storage.

The reason we are listening the change issue of input file is to catch the selected file. Nosotros can't simply bind the value like nosotros are used to (assistant in a box ring a bell 😀). More explanation will come up later in code role. The Ionic card component is used in great to differentiate the new item course.

A bit of Styling

Just curve the corners using CSS custom backdrop and some white space for breathing room.

Code Behind Firebase Storage

We are done with markup and sprinkle of style. We besides need to modify the add-on and removal of item to adjust the new requirements. But first of all let's include some new modules to app.module.ts

Later nosotros have added the modules, next to wire upwards the actual functionalities.

In above lawmaking snippet we have injected AngularFireStorage to the constructor. From line sixteen-19 is the function that is hooked to the input[file] in the markup. This function simply sets the local variable with the selected file.

Understand the catamenia of firebase storage

A little explainer volition make things easier to follow code. At commencement we need to upload the file to storage. In gild to admission the file later, we need the downloadUrl of that file.

Firebase returns certificate id when it'due south created. We will utilize this id equally a reference name of the accompanied paradigm. On line 7 we are providing id and selected to the uploadFile method.

The uploadFile methods bank check whether user has selected the file from file dialog or it was just cancelled. Adjacent on line 22 we are grabbing the reference of our folder i-east images. The child method sets the proper noun of our new file (which is the document id in this case). The put method uploads the file.

Afterward file finished uploading, than we need to grab it's downloadUrl and simply return it. Coming back to line vii, now we have the epitome url which is reused when we update document id. If user cancels the file dialog or just ignore the paradigm than fileUpload will return undefined. In other words that particular todo item volition not take the image. Just if user has selected the file than nosotros update the todo item id forth with imagUrl.

While all the code in place, we have a pick add paradigm to our new to do item. There is one more thing to do; what happens when we remove the todo item.

Remove Image from Firebase Storage

Equally we already have the reference of the image thanks to the id of associated todo list. All nosotros have to do is to call remove method firebase storage

It is quite identical to uploading the file. We fetch the reference and call the remove method, only, if detail has an image.

We have achieved the functionality up to this point. Simply uploading epitome is an asynchronous operation on its own. Which ways it will take time, it will happen at some betoken in the future. The good thing is to let user know that something happens behind the scenes.

Improve UX

Permit'due south heighten the uploading with the ionic loading component.

As we take injected the Loading Controller to the constructor.

The above code volition nowadays the loading indicator look for operation and dismiss it when nosotros are washed with it.

Conclusion

1 prissy thing well-nigh this enhancement is that it is a not destructive addition to to do items. We can choose to upload image when adding todo or skip the image selection.

Mention in the comment, what crawly software you are going to build using firebase storage. Share this article with your colleagues.

The source lawmaking is available on the github

Screencast

blaylockgrencir.blogspot.com

Source: https://www.technbuzz.com/2020/02/09/upload-photos-to-firebase-storage-with-ionic-angular/

0 Response to "How to Upload Picture to Firebase Storage"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel