You have unlimited access as a PRO member
You are receiving a free preview of 3 lessons
Your free preview as expired - please upgrade to PRO
Contents
Recent Posts
- Object Oriented Programming With TypeScript
- Angular Elements Advanced Techniques
- TypeScript - the Basics
- The Real State of JavaScript 2018
- Cloud Scheduler for Firebase Functions
- Testing Firestore Security Rules With the Emulator
- How to Use Git and Github
- Infinite Virtual Scroll With the Angular CDK
- Build a Group Chat With Firestore
- Async Await Pro Tips
Angular File Uploads to Firebase Storage
Episode 6 written by Jeff DelaneyHealth Check: This lesson was last reviewed on and tested with these packages:
- Angular v4
- AngularFire2 v4
Update Notes: Good news! Firebase Storage is now supported natively in AngularFire2. Checkout the latest AngularFire2 Storage Tutorial.
Find an issue? Let's fix it
Currently, file storage is not supported in the AngularFire2 package, but it’s still possible to use Firebase storage by interacting directly with the Firebase JavaScript API. It’s good to understand this because you will likely need work directly with the firebase API as your app grows in complexity. This lesson also uses Lodash to make iterating over multiple files easier.
Firebase Upload Demo
## How to Interact with the Firebase API in Angular
Assuming you’ve installed AngularFire2 and bootstrapped your environment API credentials, you can load the firebase web API like so.
import * as firebase from 'firebase/app'; |
Step 1: Generate the Files
The structure of this feature here is almost identical to the Angular Firebase CRUD Tutorial - the main difference being that we are uploading files to Firebase Storage before saving the details to the Firebase realtime DB.
|
Step 2: Define the Upload Class
The upload class will be used in the service layer. Notice it has a constructor for file
attribute, which has a type of File
. This will allows us to initialize new uploads with a JavaScript File object. You will see why this is important in the next step.
export class Upload { |
Step 3: Building the Upload Service
The file upload process needs the (1) upload the file and (2) save a record to the database. Let’s kick this off with the imports and constructor.
Now let’s handle the main upload process using the pushUpload
function. Here’s what’s happening step-by-step.
- Establish a reference to the firebase storage bucket.
- Define the uploadTask as a promise to put the file in storage.
- Monitor the uploadTask event using the
.on
function. - Handle the events of in progress, success, and error.
@Injectable() |
Now we can reuse this upload process for both single and multiple file uploads from the component.
Deleting Files from Storage
Deleting files is a lot easier than uploading them. Here’s the code to delete files from both firebase storage and the realtime DB. Here we have separate functions for deleting the database info and the stored file.
deleteUpload(upload: Upload) { |
Step 4: Uploading via the Frontend Components
Now we need to give users a way to choose files and upload or delete them. Let’s start with the UploadFormComponent
because that is where most of the action is happening. When a user selects files in an HTML file input, it fires the change event. Our template will listen the change event, then pass event (which contains the FileList object) to our component. We also need a couple buttons to trigger the upload process.
uploads/upload-form.ts
import { Component, OnInit } from '@angular/core'; |
uploads/upload-form.html
As an added touch, I use the snapshot of the upload progress value to adjust the width of the bootstrap progress bar.
<div *ngIf="currentUpload"> |
Step 5: Displaying Files from the Database
Looping over files follows the same process as the Realtime DB tutorial. Check out the database CRUD lesson or the full file upload code in the github repo.