Skip to content

Instantly share code, notes, and snippets.

@ryctabo
Last active October 9, 2018 19:24
Show Gist options
  • Save ryctabo/f4a60a037ae0697f97c13b1c44722cba to your computer and use it in GitHub Desktop.
Save ryctabo/f4a60a037ae0697f97c13b1c44722cba to your computer and use it in GitHub Desktop.
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
uploadProgress: Observable<number>;
uploadURL: Observable<string>;
constructor(private _storage: AngularFireStorage) { }
upload(event) {
// Get input file
const file = event.target.files[0];
// Generate a random ID
const randomId = Math.random().toString(36).substring(2);
console.log(randomId);
const filepath = `images/${randomId}`;
const fileRef = this._storage.ref(filepath);
// Upload image
const task = this._storage.upload(filepath, file);
// Observe percentage changes
this.uploadProgress = task.percentageChanges();
// Get notified when the download URL is available
task.snapshotChanges().pipe(
finalize(() => this.uploadURL = fileRef.getDownloadURL())
).subscribe();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment