Skip to content

Instantly share code, notes, and snippets.

@cryptixcoder
Forked from CristalT/FileUploader.vue
Created February 6, 2018 20:42
Show Gist options
  • Save cryptixcoder/ab19d532fcd8beede5e3bf146bfb8d9c to your computer and use it in GitHub Desktop.
Save cryptixcoder/ab19d532fcd8beede5e3bf146bfb8d9c to your computer and use it in GitHub Desktop.
File Uploader Component for Vue.js using Firebase Storage
<template>
<div>
<input type="file" multiple accept="image/*" @change="detectFiles($event.target.files)">
<div class="progress-bar" :style="{ width: progressUpload + '%'}">{{ progressUpload }}%</div>
</div>
</template>
<script>
import { storage } from '../firebase'
export default {
data () {
return {
progressUpload: 0,
file: File,
uploadTask: '',
downloadURL: ''
}
},
methods: {
detectFiles (fileList) {
Array.from(Array(fileList.length).keys()).map( x => {
this.upload(fileList[x])
})
},
upload (file) {
this.uploadTask = storage.ref('imagenes/articulos').put(file);
this.uploadTask.then(snapshot => {
this.downloadURL = this.uploadTask.snapshot.downloadURL;
this.$emit('url', this.downloadURL)
})
}
},
watch: {
uploadTask: function() {
this.uploadTask.on('state_changed', sp => {
this.progressUpload = Math.floor(sp.bytesTransferred / sp.totalBytes * 100)
})
}
}
}
</script>
<style>
.progress-bar {
margin: 10px 0;
}
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment