Last active
February 7, 2017 22:11
-
-
Save c0depanda/0a73aced33353e34cedb3452122de4fe to your computer and use it in GitHub Desktop.
Uploading Data to an api endpoint using Vue Resource and FormData Web Api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div> | |
<input type="file" @change="avatarChange" class="uploadIcon"> | |
</div> | |
</template> | |
<script> | |
export default { | |
data: function () { | |
return { | |
avatar: '' | |
} | |
}, | |
methods: { | |
avatarChange: function (e) { | |
// Fetch Data from input | |
let files = e.target.files || e.dataTransfer.files; | |
// Set files array at position 0 to local state data | |
this.avatarFiles = files[0] | |
}, | |
updateAvatar: function () { | |
// Create FromData | |
let data = new FormData(); | |
// Append file to Formdata | |
data.append('upload_avatar', this.avatarFiles); | |
// Call API Endpoint and pass formdata to it | |
this.$http.post('API ENDPOINT URL', data).then(function(response) { | |
console.log(response); | |
}).catch(function(error) { | |
console.log(error); | |
}); | |
} | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment