I'm trying to use a 'file picker' component globally on my app and getting the 'imageUrl' base64 to manipulate upload on server.
I have the following on my component:
<template>
<div class="column" style="width: 300px;">
<q-btn @click="sendEventImage ()" style="margin-top: 16px; margin-bottom: 32px;" outline color="white" text-color="black" label="Procurar imagem" />
<input style="display:none" ref="EventfileInput" @change="onEventFilePicked" type="file" name="upload" accept="image/*" />
<q-img class="w-30 h-30" :src="imageUrl" />
</div>
</template>
<script>
import { defineComponent, ref } from 'vue'
export default defineComponent({
name: 'PageIndex',
setup () {
let imageUrl = ref('')
let model = ref(null)
return {
sendEventImage () {
console.log('sendEvent preview')
this.$refs.EventfileInput.click()
},
onEventFilePicked (event) {
const files = event.target.files
let image = files[0]
console.log(image)
let filename = files[0].name
if (filename.lastIndexOf('.') <= 0) {
return alert('Por favor adicione um arquivo válido')
}
const fileReader = new FileReader()
fileReader.addEventListener('load', (event) => {
this.imageUrl = fileReader.result
console.log('setimageUrl', this.imageUrl)
})
fileReader.readAsDataURL(files[0])
},
imageUrl,
model
}
}
})
</script>
How can i use this component in every component of my app and also get the "imageUrl" parameter on my setup () to use this parameter ?