Skip to content

Instantly share code, notes, and snippets.

@omitobi
Created November 2, 2018 09:12
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 omitobi/fccf943fa43036c87feb56a5dee1418e to your computer and use it in GitHub Desktop.
Save omitobi/fccf943fa43036c87feb56a5dee1418e to your computer and use it in GitHub Desktop.
Upload Image with VueJS
<!-- See: https://jsfiddle.net/edwinencomienda/eywraw8t/444118/ -->
<div id="app">
<form @submit.prevent="onSubmit">
<input type="file" @change="onFileChange">
<button>Submit</button>
</form>
</div>
<script>
new Vue({
el: "#app",
data: {
form: {
name: '',
image: ''
}
},
methods: {
onFileChange (event) {
this.form.image = event.target.files[0]
},
onSubmit () {
const formData = new FormData
for (let key in this.form) {
formData.set(key, this.form[key])
}
// formData submit as payload like axios
// axios.post('example.com', formData)
}
}
})
</script>
<style>
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#app {
background: #fff;
border-radius: 4px;
padding: 20px;
transition: all 0.2s;
}
li {
margin: 8px 0;
}
h2 {
font-weight: bold;
margin-bottom: 15px;
}
del {
color: rgba(0, 0, 0, 0.3);
}
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment