File Upload empty on submit

Solved it by converting file into base64 string.

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');
  });

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top