I build a local server by Nodejs. All I want is to upload a file from my website. When I send a post request by axios from the website, my localhost just receive a req.body of a null object({}). I have no idea about it. Here's the code. the font end:
let fileList = this.$refs.getPic.files
let file = fileList[0]
let fileReader = new FileReader()
fileReader.readAsDataURL(file)
fileReader.addEventListener('load', () => {
let res = fileReader.result
this.picURL = res
})
const formData = new FormData()
formData.append('userPicture', file)
let config = {
headers: {
'Content-Type': 'multipart/form-data',
},
}
axios.post('http://127.0.0.1:4000/data/account/file', {formData}, config)
.then(res => {
console.log(res)
})
.catch(err => {
console.log(err)
})
the server:
router.post('/account/file', (req, res) => {
console.log(req.body) // here i get a {}
console.log(req.headers)
res.send(req.body)
})
You have to use a middleware like multer to handle this easily.