【发布时间】:2018-02-28 04:11:07
【问题描述】:
我是第一次使用 multer,遇到了一些麻烦。
我想从带有 superagent lib 的 react 客户端将图像文件上传到我的服务器上。
但req.file 数据始终未定义,这是我的代码:
服务器端:
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()
})
还有我的客户端:
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}/>
)
}
在我的超级代理请求中,我必须覆盖内容类型,否则会发送 json 数据。
但req.file 在我的后端仍未定义。
感谢您的帮助!
【问题讨论】:
-
你有没有想过这个问题?我现在也有同样的问题。
标签: javascript file express multer superagent