我无法将商店中的表单数据作为formData发布到服务器文件中。 在提交之前检查状态,我可以看到图像和视频属性具有formDatas作为值,但是在提交时,请求的参数是空对象
<template>
<input type="file" @change="onImageSelected($event)" accept="image/*" >
</template>
methods: {
submit() {
this.$v.$touch()
if (this.$v.$invalid || this.tags.length > 3) {
this.submitStatus = 'ERROR'
} else {
this.submitStatus = 'PENDING'
this.$store.dispatch('exercises/storeExercise')
}
},
onImageSelected(event) {
this.image = event.target.files[0]
if (this.image.size < 8000000) {
const formDataImage = new FormData()
formDataImage.append('image', this.image)
this.$store.dispatch('exercises/commitImageSelected', formDataImage)
}
},
}
const state = {
exercises: [],
tags: [],
selectedExercise: null,
selectedComponent: BeforeSelect
}
const mutations = {
setImageSelected(state, image) {
Vue.set(state.selectedExercise, 'image', image)
},
setVideoSelected(state, video) {
Vue.set(state.selectedExercise, 'video', video)
}
}
const actions = {
commitImageSelected({commit}, image){
commit('setImageSelected', image)
},
commitVideoSelected({commit}, video){
commit('setVideoSelected', video)
},
storeExercise({commit, state}) {
axios.post('exercises', state.selectedExercise)
.then(({data}) => {
commit('setExercises', data)
})
.catch(err => console.log(err))
},
}
答案 0 :(得分:0)
在storeExcerise方法中尝试此axios帖子。
axios.post('exercises',state.selectedExercise,{
headers: { 'Content-Type': undefined},
}).then(function (response) {
if (response.data.ok) {
}
}.bind(this));
当我将文件上传到服务器时,这对我来说很好。