Skip to content

Instantly share code, notes, and snippets.

@mrgarymartin
Created January 2, 2023 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mrgarymartin/13d69d8649048406c183a16fc38e3e43 to your computer and use it in GitHub Desktop.
Save mrgarymartin/13d69d8649048406c183a16fc38e3e43 to your computer and use it in GitHub Desktop.
Angular Fire Upload tasks
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