Created
January 2, 2023 18:33
-
-
Save mrgarymartin/13d69d8649048406c183a16fc38e3e43 to your computer and use it in GitHub Desktop.
Angular Fire Upload tasks
This file contains hidden or 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
pushFileToStorage(fileUpload: FileUpload) { | |
const filePath = `${this.basePath}/${fileUpload.file.name}`; | |
const storageRef = ref(this.storage, filePath); | |
const uploadTask = uploadBytesResumable(storageRef, fileUpload.file); | |
uploadTask.on('state_changed', | |
(snapshot) => { | |
// Observe state change events such as progress, pause, and resume | |
// Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded | |
const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100; | |
console.log('Upload is ' + progress + '% done'); | |
switch (snapshot.state) { | |
case 'paused': | |
console.log('Upload is paused'); | |
break; | |
case 'running': | |
console.log('Upload is running'); | |
break; | |
} | |
}, | |
(error) => { | |
alert(error); | |
}, | |
() => { | |
getDownloadURL(uploadTask.snapshot.ref).then((downloadURL) => { | |
console.log('File available at', downloadURL); | |
fileUpload.url = downloadURL; | |
fileUpload.name = fileUpload.file.name; | |
this.saveFileData(fileUpload); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment