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>
  
Giovanni
  • 512
  • 2
  • 6
  • 23
  • Does this answer your question? [How to upload files using React?](https://stackoverflow.com/questions/46233585/how-to-upload-files-using-react) – Liam Apr 24 '23 at 14:25
  • your event should be `onChange={this.handleUpload.bind(this)}` – Liam Apr 24 '23 at 14:26

0 Answers0