Im using multer for the first time and I have some trouble with it.
I would like to upload image file on my server from a react client with superagent lib.
But the req.file
data is always undefined, here is my code :
Server side :
const upload = multer({
dest: 'uploads/' })
app.post('/uploadprofile', upload.single('profil'), (req, res) => {
console.log(req.file);
console.log(req.files);
console.log(req.body);
res.status(200).end()
})
And my client side :
onUploadFile(e) {
console.log(e.target.files[0])
this.setState({
img: e.target.files[0]
}, () => {
agent
.post("http://localhost:3100/uploadprofile")
.attach('profil', this.state.img, this.state.img.name)
.set("Content-Type", "")
.end((err, res) => {
console.log(err)
console.log(res)
console.log("send")
})
})
}
render() {
return (
<input id="file" type="file" accept="image/*" name="profil" className="inputButton" onChange={this.onUploadFile}/>
)
}
In my superagent request, I have to overwrite the content-type, otherwise json data are send.
But the req.file
is still undefined on my backend.
Thanks for the help !