【问题标题】:Bus Boy on file not firing档案中的巴士男孩没有开火
【发布时间】:2022-09-29 16:13:17
【问题描述】:

在 Next Js 中,我有一个上传图片端点。

const busboy = require(\'busboy\')

export default function handle(req, res)
{
    const bb = busboy({headers: req.headers});
    bb.on(\'file\', (fieldname, file, filename, encoding, mimetype) => 
    {
        res.status(200).send({ message: \'file\' });  //doesn\'t do this
    });
    bb.on(\'close\', () =>
    {
        res.status(201).send({ message: \'close\' });
    });
    bb.on(\"finish\", () => 
    {
        res.status(202).send({ message: \'finish\' });
    });
    req.pipe(bb);
    res.status(203).send({ message: \'failed\' });
}

我正在尝试解析表单数据,但 busboy 似乎没有输入文件、关闭或完成。它会跳过它们并返回 203 failed 消息。

这是我的下一个 onsubmit 事件,它调用这个端点

function onFileUpload(e)
  {
    e.preventDefault();
    let form = new FormData();
    form.append(\"image\", file, file.name);
    console.log(form)
    axios({
      method: \"post\",
      url: \"http://localhost:3000/api/uploadImage\",
      data: form,
      headers: form.getHeaders
      //{\'Content-Type\': \'multipart/form-data\'}
    })
    .then((res) =>
    {
      console.log(res);
    });
  }

有谁知道问题是什么?

    标签: javascript api next.js busboy


    【解决方案1】:

    我在 GCP 云功能上遇到了与 Busboy 类似的问题。解决我的问题是在“文件”回调注册后添加bb.end(req.rawBody);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-15
      • 2020-06-28
      • 1970-01-01
      • 1970-01-01
      • 2020-09-06
      • 1970-01-01
      相关资源
      最近更新 更多