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

why i cant upload file in reactjs

My File is shown in the console but getting a 400 error i.e. Please upload the file.

    function AdminPanel() {
      const [show, setShow] = useState(false);
      const [pdffile, setPdffile] = useState("");

   function handleFileChange(e) {
      const files = e.target.files[0];
       setPdffile(files);
       console.log("Guru4666", pdffile);
   }
  const SubmitPDF = () => {
     axios({
     method: "post",
     url: "url",
     data: {"dataFile" : pdffile }
  }).then(res => {
    console.log("REs", res)
  })
}

<div className="form-group form_aside">
    <input type="file" className="form-control" id="files"
     name="PDF" onChange={handleFileChange} />
</div>
                          

Submit

Console result is

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

Guru4666 
File {
lastModified: 1634196746000
lastModifiedDate: Thu Oct 14 2021 13:02:26 
GMT+0530 (India Standard Time) {}
name: "barkelou.png"size: 5906type: 
"image/png"webkitRelativePath: ""
[[Prototype]]: File}

And the API result is: –

dataFile: {}

400

Please help, I need urgent help.

>Solution :

You should probably use FormData and multipart/form-data

 const SubmitPDF = () => {
   const formData = new FormData();
   formData.append("dataFile", pdffile);
   axios({
     method: "post",
     url: "url",
     data: formData,
     headers: {
      'Content-Type': 'multipart/form-data'
    }
  }).then(res => {
    console.log("REs", res)
  })
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