首页 > 解决方案 > 上传到 Next.js API 时损坏的图像

问题描述

我正在尝试将图像作为表单数据上传到 Nextjs api 路由。我使用强大的包来解析文件并将其保存在服务器文件夹中。对于 http 请求,我在客户端使用邮递员。

这是后端代码:

import formidable from 'formidable';

export const config = {
  api: {
    bodyParser: false,
  },
};

export default async (req, res) => {
  const form = new formidable.IncomingForm();

  form.on('fileBegin', (name, file) => {
    file.path = "./" + file.name

  });

  form.parse(req, (err, fields, files) => {
    console.log( files);
  });

  res.statusCode = 200
  res.end()
};

图像 (jpeg) 保存在文件夹中。但是,它似乎已损坏或损坏。这是原始图像:

源图像

损坏的图像

标签: multipartform-datanext.jscorruptformidable

解决方案


Next.js 需要包formidable-serverless而不是formidable

https://github.com/node-formidable/formidable/issues/629


推荐阅读