我正在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()处理,因为处理功能依赖于它.有什么办法吗?

推荐答案

看一看this

由于您的要求涉及处理来自<input type="file"/>的图像文件和来自<input type="text"/>的图像URL,并且考虑到try 直接上传图像时使用POST方法所面临的问题,我建议使用multiparty:

import { NextApiRequest, NextApiResponse } from 'next';
import multiparty from 'multiparty';

export const config = {
  api: {
    bodyParser: false, // Disable the default body parser
  },
};

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
  if (req.method === 'POST') {
    const form = new multiparty.Form();
    form.parse(req, async (error, fields, files) => {
      if (error) {
        res.status(500).json({ message: 'Form parsing error' });
        return;
      }
      const { path: imagePath } = files.image[0]; // Adjust based on your input's name attribute
      const imageBuffer = await fs.promises.readFile(imagePath);
      const result = await processing(imageBuffer); // Adjust your processing function to handle Buffer
      res.status(200).json({ message: 'Done', ...result });
    });
  } else {
    // Handle other methods or return an error
    res.setHeader('Allow', ['POST']);
    res.status(405).end(`Method ${req.method} Not Allowed`);
  }
}

Javascript相关问答推荐

在JavaScript中对大型级联数组进行切片的最有效方法?

对象和数字减法会抵消浏览器js中的数字

确定MutationRecord中removedNodes的索引

我在这个黑暗模式按钮上做错了什么?

窗口.getComputedStyle()在MutationObserver中不起作用

单击子元素时关闭父元素(JS)

使用JQuery单击元素从新弹出窗口获取值

使用Java脚本导入gltf场景并创建边界框

如何在Java脚本中对列表中的特定元素进行排序?

警告框不显示包含HTML输入字段的总和

为列表中的项目设置动画

如何使用puppeteer操作所有选项

如何压缩图像并将其编码为文本?

Django模板中未加载JavaScript函数

有没有办法在R中创建一张具有多个色标的热图?

我如何才能获得价值观察家&对象&S的价值?

脚本语法错误只是一个字符串,而不是一个对象?

将字体样式应用于material -UI条形图图例

使用Java脚本在div中创建新的span标记

如何从图表中映射一组图表-js使用REACT