Skip to content

Instantly share code, notes, and snippets.

@TakuyaHarayama
Created January 11, 2019 01:13
Show Gist options
  • Save TakuyaHarayama/15d286f6b973b8a11740d121a20a7963 to your computer and use it in GitHub Desktop.
Save TakuyaHarayama/15d286f6b973b8a11740d121a20a7963 to your computer and use it in GitHub Desktop.
vueファイルアップロード
<template>
<div>
<input type="file" multiple accept="image/jpeg" @change="detectFiles($event.target.files)">
<div class="progress-bar" :style="{ width: progressUpload + '%'}">{{ progressUpload }}%</div>
<img v-bind:src="image"/>
</div>
</template>
<script>
import firebase from 'firebase'
export default {
name: 'Upload',
data () {
return {
progressUpload: 0,
file: File,
uploadTask: '',
image: ''
}
},
methods: {
detectFiles (fileList) {
Array.from(Array(fileList.length).keys()).map( x => {
this.upload(fileList[x])
})
},
upload (file) {
this.uploadTask = firebase.storage().ref('uploads/' + file.name).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)
this.image = downloadURL
})
})
}
}
}
</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