Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple React file upload, form data not appending

     const [image, setImage] = useState({ preview: "", file: "" });
     const handleChange = (e) => {
        e.preventDefault();
        if (e.target.files.length) {
          setImage({
            preview: URL.createObjectURL(e.target.files[0]),
            file: e.target.files[0],
          });
        }
      };
      useEffect(() => {
        const formData = new FormData();
        formData.append("file", image.file);
        console.log(formData);
      }, [image]);

In the above code console.log(formData); returns empty object, unable to send file on axios

like image 478
Sudhakar Behera Avatar asked Oct 11 '25 16:10

Sudhakar Behera


1 Answers

You cannot print FormData to see the entries. https://developer.mozilla.org/en-US/docs/Web/API/FormData

You need to get it with key: formData.get('file') should give you what you need to verify.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!