我有一个存储为 Blob 的数据图像,但我不知道如何使用 Axios 发布它,我使用 VUEJS。请帮助我。
<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/