Vue使用axios上传文件

<template>
  <div class="upload-demo">
    <input type="file" ref="fileInt" @change="changeHandle">
  </div>
</template>
 
<script>
 
export default {
  name: 'UploadFileDemo',
  props: {
    msg: String
  },
  methods: {
    changeHandle() {
      const file = this.$refs.fileInt.files[0];
      console.log(file);
      const data = new FormData();
      data.append('file', file);
      axios.post('http://localhost:3006/common/upload', data, {
        headers: {
          'Content-Type': 'multipart/form-data',
        },
      }).then(res => {
        console.log(res);
      }).catch(err => {
        console.log(err);
      });
    }
  },
}
</script>
 
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>

猜你喜欢

转载自www.cnblogs.com/Narkea/p/10778255.html