Last active
October 5, 2023 11:20
-
-
Save CristalT/2651023cfa2f36cddd119fd979581893 to your computer and use it in GitHub Desktop.
File Uploader Component for Vue.js using Firebase Storage
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
<template> | |
<div> | |
<input type="file" multiple accept="image/jpeg" @change="detectFiles($event.target.files)"> | |
<div class="progress-bar" :style="{ width: progressUpload + '%'}">{{ progressUpload }}%</div> | |
</div> | |
</template> | |
<script> | |
import storage from 'firebase/storage' | |
export default { | |
data () { | |
return { | |
progressUpload: 0, | |
file: File, | |
uploadTask: '', | |
} | |
}, | |
methods: { | |
detectFiles (fileList) { | |
Array.from(Array(fileList.length).keys()).map( x => { | |
this.upload(fileList[x]) | |
}) | |
}, | |
upload (file) { | |
this.uploadTask = storage.ref('imagenes').put(file); | |
} | |
}, | |
watch: { | |
uploadTask: function() { | |
this.uploadTask.on('state_changed', sp => { | |
this.progressUpload = Math.floor(sp.bytesTransferred / sp.totalBytes * 100) | |
}, | |
null, | |
() => { | |
this.uploadTask.snapshot.ref.getDownloadURL().then(downloadURL => { | |
this.$emit('url', downloadURL) | |
}) | |
}) | |
} | |
} | |
} | |
</script> | |
<style> | |
.progress-bar { | |
margin: 10px 0; | |
} | |
</style> |
wow hard
This won't work as expected, because you have
multiple
in input and each timethis.upload()
is called you are overwritingthis.uploadTask
Yes, you are right, but could you tell me the solution?
Because for example: I want to upload 3 files to firebase storage, it is successful, but i can only read the last file's downloadUrl...
Thanks
This is how the upload method needs to be written for it to work.
upload(file) {
this.uploadTask = storage
.ref('images')
.child(file.name)
.put(file)
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi CristalT and Crew,
I found information on FileUploader.vue on website: 'https://lovemewithoutall.github.io/it/vue-image-upload-to-firestorage/ ', which gave Thanks to your github repo.
I am trying to get it to work with my project. I refactored this code into mine, but having a difficult time understanding what is wrong. Can you help me? Here is my github repo: https://github.com/gchan82/feedme . I put this material on FileUploaderMain.vue and child is FileUploader.vue. Hierarchy is like this:
AddDish.vue
FileUploaderMain.vue
FileUploader.vue
Maybe you can respond back on my github repo under issues. First time using this comment board, so I don't know if/where I will get response back. Thank you.