Skip to content

Instantly share code, notes, and snippets.

@CristalT
Created April 12, 2019 04:38
Show Gist options
  • Save CristalT/c9bc98a2257a4ec70521a540498f05bc to your computer and use it in GitHub Desktop.
Save CristalT/c9bc98a2257a4ec70521a540498f05bc to your computer and use it in GitHub Desktop.
Extension for QUploader component making it able to upload files throw Firebase Storage
<template>
<q-uploader-firebase :ref-storage="refStorage" @uploaded="setImage" />
</template>
<script>
import QUploaderFirebase from '...'
export default {
components: {
QUploaderFirebase
},
data() {
return {
refStorage: firebase.storage.ref('images')
}
},
methods: {
setImage(downloadURL) {
// ...
}
}
}
import { QUploaderBase } from 'quasar'
export default {
props: ['refStorage'],
name: 'FirebaseUploader',
data () {
return {
uploading: '',
uploadTask: {}
}
},
mixins: [QUploaderBase],
computed: {
isIdle() {
return !this.uploading
},
isUploading() {
return this.uploading
}
},
methods: {
abort() {
if (!this.disable && this.isUploading) {
this.uploadTask.cancel()
this.uploading = false
}
},
upload() {
if (this.disable || !this.queuedFiles.length) {
return
}
const queue = this.queuedFiles.slice(0)
this.queuedFiles = []
queue.forEach(file => {
this.uploadFile(file)
})
},
uploadFile(file) {
const ref = this.refStorage
this.uploadTask = ref.child(file.name).put(file)
this.uploadTask.on(
'state_changed',
snapshot => {
this.uploading = true
this.uploadSize = snapshot.totalBytes
this.uploadedSize = snapshot.bytesTransferred
},
error => {},
() => {
this.uploadTask.snapshot.ref.getDownloadURL().then(downloadURL => {
this.uploading = false
this.$emit('uploaded', downloadURL)
})
}
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment