如何在axios中POST formData和其他数据



如果它只是formData,我可以发送它。但是如果你把其他数据放在一起,formData将是空的。

const formData = new FormData();
formData.append('file', files);
axios.post('/api/upload', {
formData,
route: this.$route.params.test
}, {
headers: {
'content-type': 'multipart/form-data',
}}).then(response => {
console.log(response);
});

有办法解决这个问题吗?

你应该这样使用

const formData = new FormData();
formData.append('file', files);
axios.post('/api/upload', {
data:formData,
/* what is this ? If you want this to be on form data use formData.append('route', this.$route.params.test) */
headers: {
'content-type': 'multipart/form-data',
}}).then(response => {
console.log(response);
});

最新更新