我正在try 使用next.js API,但遇到了一些问题.我有一个<input type="file"/>
和一个正常的<input type="text"/>
,我可以 Select 一个图像或粘贴图像的链接.提交后,基于我 Select 的任何选项,我有以下if语句:
if (typeof imageUrl == "object") {
result = await fetch(`/api/imageProcessing`, {
method: "POST",
body: imageUrl,
});
} else {
result = await fetch(
`/api/imageProcessing?image=${encodeURIComponent(imageUrl)}`,
{
method: "GET",
}
);
}
GET方法可以工作,但是POST方法不行,因为我需要.blob()
图像.我得到的错误是:
‘body.blob()不是函数’
export default async function handler(req, res) {
const { method, body, query } = req;
switch (method) {
case "GET":
const imageUrl = query.image;
if (!imageUrl) {
res.status(400).json({ message: "Missing image url parameter" });
}
const imageFile = await fetch(imageUrl).then((res) => res.blob());
const result = await processing(imageFile);
res.status(200).json({ message: "done", ...result });
break;
case "POST":
if (body) {
// const imageFile = await body.blob();
const result = await processing(imageFile);
res.status(200).json({ message: "done", ...result });
} else {
res
.status(400)
.json({ message: "Missing image file in the request body" });
}
break;
default:
res.status(405).json({ message: "Method Not Allowed" });
break;
}
}
我真的不知道如何对其进行.blob()
处理,因为处理功能依赖于它.有什么办法吗?