-
-
Save ryctabo/f4a60a037ae0697f97c13b1c44722cba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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