-
-
Save cryptixcoder/ab19d532fcd8beede5e3bf146bfb8d9c to your computer and use it in GitHub Desktop.
File Uploader Component for Vue.js using Firebase Storage
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
<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