1

I cannot upload files in React.js with FormData. Basically my console.log is empty when I try to check the formData or formData.entries(), why?

  const FileUpload = ({ src, sendImage, input }) => {
  const [source, setSource] = useState();

  const handleUpload = (e) => {
    const file = e.target.files;
    const url = URL.createObjectURL(file[0]);
    const fileName = file[0].name;
    setSource(url);
    const formData = new FormData();
    formData.append("image", url);
    
    console.log(formData.entries()); 
    sendImage({ url: formData, fileName: fileName, mime: type });
  };

  return (
      <Button variant="contained" component="label" sx={{ marginTop: "15px", borderRadius: 2 }}>
        <input hidden accept={input === "audio" ? "audio/*" : "video/*"} type="file" onChange={handleUpload} />
        UPLOAD
      </Button>
  
2
  • Does this answer your question? How to upload files using React? Commented Apr 24, 2023 at 14:25
  • your event should be onChange={this.handleUpload.bind(this)} Commented Apr 24, 2023 at 14:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.