【问题标题】:Corrupt image when uploading to Next.js API上传到 Next.js API 时损坏的图像
【发布时间】:2020-05-13 23:19:36
【问题描述】:

我正在尝试将图像作为表单数据上传到 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

【问题讨论】:

    标签: multipartform-data next.js corrupt formidable


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2020-01-09
      • 2022-08-13
      • 2016-04-14
      • 2021-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多