Vue使用axios上传文件

  • A+
所属分类:vue

<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>

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: