I am new to using multer and experiencing some difficulties with it.
My goal is to upload an image file from a react client using the superagent library to my server.
However, the req.file
data always shows as undefined in my code:
On the 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 on the 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}/>
)
}
I had to override the content-type in my superagent request or else JSON data would be sent instead.
Despite making these changes, the req.file
remains undefined on the server side.
Any help or suggestions would be greatly appreciated!