banner



How To Connect Image Upload To Firebase Ionic 3

How to Upload Files from Ionic to Firebase Storage

Firebase tin exist used in a lot of means, and too the database the second biggest feature might be its deject storage.

In this tutorial we volition go on with an app from a previous tutorial on using different media files with Ionic. Y'all can grab the code for the previous app and apply the changes of this post, or just wait how the file upload could work in general!

Finally we will be able to capture media files, store them on our device so upload them to Firebase storage!
upload-ionic-files-firebase-storage

For this app you will also need a Firebase account and an app inside the panel – both are gratis so go alee and create them!

Preparing our Media App

The showtime change nosotros make to our previous project is adding Firebase, and some other page where nosotros tin display all files that we uploaded. Therefore run within the binder of the previous project:

The InAppBrowser will exist used to showcase that nosotros actually got a real URL to the files after uploading them – of course this is not really mandatory otherwise.

For our simple instance we will also disable the security rules on our Storage inside Firebase, so navigate to Storage -> Rules and commutation the current rules for these, which allow read and write admission to everyone:

You should of course change these after testing and have advisable security rules in place in a existent app!

Calculation AngularFire and Firebase

Now information technology'south time to connect our Ionic app to Firebase, and for this you demand the credentials of your Firebase app. You lot can add a new app to your Firebase project by clicking at the peak left on the wheel and so Project settings. At the bottom of that page you tin can edit your apps or add a new web project.

One time you created this yous need to copy the configuration which looks like this:

ionic-4-firebase-add-to-app

We can re-create information technology out and paste it into our environments/surround.ts and brand it look like this (only of grade with your values inside):

With that information in place we tin can initialise the AngularFire module inside our app/app.module.ts and also import the AngularFireStorageModule which is needed to access the storage:

Now our app is connected with Firebase and set to upload some files!

Uploading Files to Firebase Storage

In the previous tutorial nosotros captured files and stored them on our device, so we take all data locally bachelor. At present we want to upload a certain file from a path, and nosotros can do this by converting information technology to a Blob!

The Cordova file plugin already comes with a helpful function called readAsArrayBuffer that will read a local file, but we likewise demand to brand sure that we ready the correct mimetype for the file.

To practice so, we add a simple helper function that checks the extension of the file – of form yous might take dissimilar files so feel gratuitous to extend that function to your needs.

Side by side we create a random id and combine it with a date to create a uniqueish name for our file inside Firebase storage.

The rest becomes super piece of cake: We simply telephone call the upload function on the AngularFireStorage and pass the new name (or path to the file) plus the blob we created. This volition return an UploadTask, which offers some absurd functionality like subscribing to the changes in club to display a progress bar!

Now go alee and change your previous app/dwelling/home.page.ts to include the new functionality also:

Inside the view we already had a list previously, and we just add the progress bar and another sliding button to perform the upload chore, and then go ahead and change the app/home/habitation.page.html to:

Now yous can run your app on a device, capture a media file of any type (that will be stored on the device first) so call the deject upload to send it to Firebase Storage!

You can see the result past going to the Storage expanse of your Firebase app afterwards you uploaded a file.

Working with Files in Firebase Storage

If you also want to list all files in your app that yous've uploaded to Firebase, you tin do information technology every bit well. At the time writing this it was not possible through AngularFire, simply you can besides directly utilise the spider web SDK of Firebase for cases similar this.

All you have to do is call listAll on the reference to your storage folder and it will render a list of references. These references contain a lot of data, and we just extract the interesting fields and push it to our local array which we will use within our view in the next footstep.

The most interesting part here is perhaps the getDownloadURL, which returns a Promise. That'due south why we have to wait for it (or use the async pipe in the view, any you prefer), but this volition render the full URL to the hosted file on Firebase!

Go ahead and change your app/deject-listing/cloud-listing.page.ts to:

This means we can use this URL in our app to display files directly from Storage, or like we do, even open up an external browser with the URL!

Finally, y'all tin can also call different functions on the reference itself like we do in the delete function.

To wrap things up, we build a super simple list around the array within the app/cloud-list/cloud-list.folio.html like this:

Now you can upload your files, see which files were uploaded, delete them or even employ their full path to the hosted file.

Conclusion

Working with Firebase storage is a great way to hands host different files that your app needs, like user avatars or other uploaded files.

The biggest challenge might really be the conversion from file to hulk, but with the functions of this tutorial you lot should be prepared to handle everything!

Y'all can as well discover a video version of this commodity beneath.

How To Connect Image Upload To Firebase Ionic 3,

Source: https://devdactic.com/upload-ionic-files-firebase-storage/

Posted by: donnersily1998.blogspot.com

0 Response to "How To Connect Image Upload To Firebase Ionic 3"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel