我正在尝试在下一个 js 应用程序中将文件发送到 api。图像将上传到 cloudinary:
函数调用api是:
async uploadButtonClicked(imageUpload) {
const formData = new FormData();
//formData.append('test', "testing");
formData.append('postPic', imageUpload.files[0]);
const res = await fetch('/api/hello', {
method: 'POST',
headers: {
'Content-Type': 'multipart/form-data',
'Accept': 'application/json'
},
body: formData,
})
console.log(imageUpload.files[0])
在前端给了我下面的值,看起来不错。
在 API 中,
export default (upload.single('postPic'), async (req, res) => {
console.log(req.body)
以上是undefined
我使用时
export const config = {
api: {
bodyParser: false,
},
};
当我删除 bodyParser 设置(bodyParser 为 true)时,数据是一个对上传没有用的流。我收到上传错误,如下所示:
如果正文以以下格式到达 api,Cloudinary 将上传:
应该更改什么才能使主体(基本上是图像文件)以正确的格式到达 api,如上所示?
我认为这个问题也可以问为:为什么req.body undefined
使用时bodyParser: false
?