【发布时间】:2019-06-25 10:03:06
【问题描述】:
我正在尝试使用 react 前端和 express 服务器将图像上传到 cloudinary。 问题是我无法正确地将请求图像发布到我的快递服务器。
这就是我准备图像以稍后发送的方式:
var data = new FormData();
console.log(event.target.files[0]) // this prints FileObject succesfully
data.append('image', event.target.files[0]);
console.log(data) // this prints {} but i guess its natural since its FormData ??
this.imageToUpload = data;
这是我发布请求的方式:
axios.post('/api/courses/uploadImage',this.imageToUpload, {
headers: {
'Content-Type': 'multipart/form-data'
}
})
.then( (response) => {
alert(JSON.stringify(response));
})
.catch(function (error) {
console.log(error);
});
现在在服务器端,req.body 为空。
router.post("/courses/uploadImage",(req,res,next)=>{
console.log(req.body) // empty
var image = req.body;
cloudinary.uploader.upload(image, function(error, result) { console.log(result) });
})
另外,我应该将什么真正放入(在这种情况下为图像)uploader.upload 的第一个参数?
【问题讨论】:
-
您尝试在uploader.upload 中使用req.file.path 吗?使用 body-parser 也可能有所帮助:stackoverflow.com/questions/9177049/…
-
req.file 未定义
标签: javascript reactjs express axios cloudinary