Last active
March 2, 2019 08:11
-
-
Save panw3i/9396e4f4f61df60f8a40e94787481b45 to your computer and use it in GitHub Desktop.
Vue上传文件
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 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