2 回答
TA贡献1794条经验 获得超8个赞
通过将文件转换为 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条经验 获得超8个赞
所有数据都必须添加到 FormData 对象中,要以类似数组的形式获取数据,您可以尝试方括号表示法。
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');
});
};
添加回答
举报