Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

FormData not letting me append file in React Js using typescript

I want to send a File in my request using React Js, but I am unable to append file in object of FormData using typescript.
Here’s my React Js Code

    const [imageFile, setImageFile] = React.useState();
      const [productImageMutation, productImageMutationResult]=useAddProductImageMutation();
      const handleUploadClick = (e: any) => {
      var file = e.target.files[0];
      setImageFile(file);
  };
  const handleOnSubmit = (values: Picture) => {
      const formData = new FormData();
      formData.append("formFile", imageFile); //It Gives Error there.
    productImageMutation({ data: formData});
  };

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try to update your state initialization

const [imageFile, setImageFile] = React.useState('');

And also mention a type to you state because in TS you must provide a type

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading