Skip to content

Instantly share code, notes, and snippets.

@martinyung
Last active September 9, 2017 05:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinyung/04efe33be027cc8246ea366886026f28 to your computer and use it in GitHub Desktop.
Save martinyung/04efe33be027cc8246ea366886026f28 to your computer and use it in GitHub Desktop.
[blogging purpose] file upload vuejs component - js
<script>
const BASE_URL = "[YOUR_PROJECT_URL]"
export default {
name: 'FileUpload',
directives: {
uploader: {
bind (el, binding, vnode) {
el.addEventListener('change', e => {
vnode.context.file = e.target.files[0]
})
}
}
},
methods: {
selectFile () {
this.$refs.file.click()
},
uploadFile () {
let formData = new FormData()
formData.append('file', this.file)
this.$http.post(BASE_URL + '/upload', formData, {
before: request => {
this.request = request
}
})
}
},
data () {
return {
file: ''
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment