React Native POST“ multpart form
[当我尝试将映像发布到我的Node / Express服务器(在Heroku上部署时,则出现500个服务器错误。
通过邮递员投递就可以了。
这是我的React Native代码:
upload = async uri => {
const formData = new FormData();
formData.append("photo", {
uri,
name: `photo`,
type: `image/jpg`
});
const options = {
method: "POST",
body: formData,
headers: {
"Content-Type": "multipart/form-data"
}
};
axios
.post("", formData, options)
.then(response => {
console.warn(response.data);
})
.catch(error => {
console.warn("error uploading to OCR", error);
});
};
我的Express路线如下:
router.post(
"/",
multer.single("image"),
gcsMiddlewares.sendUploadToGCS,
(req, res, next) => {
console.log("post");
if (req.file && req.file.gcsUrl) {
return res.send(req.file.gcsUrl);
}
return res.status(500).send("Unable to upload");
}
);
当我不使用邮递员时,路由中的console.log不会被命中。
回答如下:您已经在发送数据。您无需将数据放入选项。
const options = {
method: "POST",
headers: {
"Content-Type": "multipart/form-data"
}
};
axios
.post("https://prezzi-api.herokuapp/upload", formData, options)
或您可以使用fetch
fetch("https://prezzi-api.herokuapp/upload", {
method: "POST",
body: formdata,
headers: {
"Content-Type": "multipart/form-data"
}
})
.then(res => res.json())
.then(response => {
...
}