Issue uploading an excel file from React to Nodejs

Viewed 41

I have written the following code in React

let onChange1 = (e) =>{
    let files=e.target.files[0]
    console.warn(' data file', files)

    let formdata = new FormData()
    formdata.append('excel file', files)

    axios({
        url: 'http://localhost:3000/excel',
        method: "POST",
        headers:{
            authorization: "Bearer " + token    
        },
        data: formdata
    })
    .then(()=>{
        console.log('success')
    })
}
 //inside return
 <input type='file' name='file' onChange={onChange1}/>

And the following in Nodejs

 stockDataRouter.route('/excel')
.options(cors.corsWithOptions, (req,res)=>{ res.sendStatus(200)})
.post(cors.cors,(req,res)=>{
    console.log(req.body)
    res.sendStatus(200)
})

I send the req from React to Nodejs and if I look into chrome dev tools I can clearly see that there is a formdata 'excel file' in the request. But then the Req.body is empty when I try to log it in NodeJs. Could you tell me if there is an issue with my code and how to solve it?

0 Answers
Related