2 回答

TA貢獻1794條經(jīng)驗 獲得超8個贊
通過將文件轉(zhuǎn)換為 base64 字符串解決了這個問題。
function getBase64(file) {
return new Promise(function(resolve, reject) {
const reader = new FileReader();
reader.onload = function() {
resolve(reader.result);
};
reader.onerror = reject;
reader.readAsDataURL(file);
});
}
const [form, setForm] = useState([]);
const fileFound = e.target.type === 'file' && e.target.files[0];
const promise = fileFound && getBase64(fileFound);
promise.then(function(result) {
setForm([
...form,
{
UUID_Answer: 'image_name,
Answer: '',
Document: true,
Document_Upload: result,
},
]);
});
const submitData = {
UUID_Formulier: '117F994F-F803-7249-91E9-EE1E7B691DFF',
answers: form,
};
axios
.post('/submit', submitData)
.then(() => {
console.log('success');
})
.catch(() => {
console.log('failed error');
});

TA貢獻1827條經(jīng)驗 獲得超8個贊
所有數(shù)據(jù)都必須添加到 FormData 對象中,要以類似數(shù)組的形式獲取數(shù)據(jù),您可以嘗試方括號表示法。
const formData = new FormData();
formData.append('UUID_Formulier', '117F994F-F803-7249-91E9-EE1E7B691DFF');
formData.append('answers[0][document]', submitFile);
formData.append('answers[0][Answer_name]', 'image');
formData.append('answers[0][Document]', true);
formData.append('answers[0][Answer]', 'Got some data');
console.log(formData) // I get formData data
axios
.post('/submit', formData, {
headers: {
'Content-Type': 'multipart/form-data',
},
})
.then(() => {
console.log('success');
})
.catch(() => {
console.log('failed error');
});
};
添加回答
舉報