上传到Next.js API时图像损坏

时间:2020-05-13 23:19:36

标签: multipartform-data next.js corrupt formidable

我正在尝试将图像作为表单数据上传到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)保存在文件夹中。但是,它似乎已损坏或损坏。 这是原始图片:

source image

corrupt image

1 个答案:

答案 0 :(得分:0)

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

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