POST Image which store as a Blob with Axios - VUEJS

自古美人都是妖i 提交于 2019-12-01 21:39:28

问题


I have a data Image which store as a Blob but I dont know how to post it with Axios, I use VUEJS. Please help me.

My Object API by VueDevtool

<file-upload v-model="files"></file-upload>
<button type="submit" v-on:click.prevent="Submit">Submit</button>

<script>
  methods: {
    data: function () {
      return {
        config: {
          'headers': {'Authorization': 'JWT ' + this.$store.state.token},
          'Content-Type': 'multipart/form-data'
        }
    },
    methods:{
      for (var file in this.files) {
        let data = new FormData()
        data.append('image', this.file[0])
        data.append('caption', 'image')
        data.append('user', this.Authuser)
        api.post('/photos/create/', data, this.config)
      }
    }
  }
</script>

回答1:


You are almost there. The only thing you need is to append the actual file and you should pass $event to your function as: Submit($event)

Submit(event) {
  let URL = '....'

  let data = new FormData()

  data.append('name', 'image')
  data.append('file', event.target.files[0])

  let config = {
    header : {
      'Content-Type' : 'multipart/form-data'
    }
  }

  axios.post(URL, data, config).then(response => {
    console.log('response', response)
  }).catch(error => {
    console.log('error', error)
  })
}


来源:https://stackoverflow.com/questions/47809402/post-image-which-store-as-a-blob-with-axios-vuejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!