vue.js - 使用 Axios - VUEJS 存储为 Blob 的 POST 图像

标签 vue.js vuejs2 axios

我有一个存储为 Blob 的数据图像,但我不知道如何使用 Axios 发布它,我使用 VUEJS。请帮助我。

VueDevtool 的我的对象 API enter image description here

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

最佳答案

你快到了。您唯一需要的是附加实际文件,您应该将 $event 传递给您的函数: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)
      })
    }

关于vue.js - 使用 Axios - VUEJS 存储为 Blob 的 POST 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47809402/

相关文章:

javascript - 更改函数中的对象变量地址

Laravel vue - 错误删除 `␍` 更漂亮/更漂亮,可能可以使用 `--fix` 选项修复

netbeans-8 - Netbeans 8.2 中是否有可用于 VueJs 的插件

ios - 无法上传图片(React native,Laravel)

reactjs - AXIOS 请求方法更改为 'OPTIONS' 而不是 'GET'

javascript - 在 Promise 上使用 apply() 且无法访问 Promise 响应数据

javascript - Vue.js:表格标题宽度取决于数据单元格

javascript - 评估注入(inject)了 v-html 的 HTML

Vue.js:仅当用户单击按钮时才加载组件

vuejs2 - 将数据传递给动态创建的 sibling