Skip to content

Instantly share code, notes, and snippets.

@panw3i
Last active March 2, 2019 08:11
Show Gist options
  • Select an option

  • Save panw3i/9396e4f4f61df60f8a40e94787481b45 to your computer and use it in GitHub Desktop.

Select an option

Save panw3i/9396e4f4f61df60f8a40e94787481b45 to your computer and use it in GitHub Desktop.
Vue上传文件
<template>
<div class="container">
<div class="large-12 medium-12 small-12 cell">
<label>File
<input type="file" id="file" ref="file" v-on:change="handleFileUpload()"/>
</label>
<button v-on:click="submitFile()">Submit</button>
</div>
</div>
</template>
<script>
export default {
data(){
return {
file: ''
}
},
methods: {
submitFile(){
let formData = new FormData();
formData.append('file', this.file);
axios.post( '/single-file',
formData,
{
headers: {
'Content-Type': 'multipart/form-data'
}
}
).then(function(){
console.log('SUCCESS!!');
})
.catch(function(){
console.log('FAILURE!!');
});
},
handleFileUpload(){
this.file = this.$refs.file.files[0];
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment