【问题标题】:req.files keeps returning undefinedreq.files 不断返回 undefined
【发布时间】:2021-11-03 16:57:01
【问题描述】:

req.files 不断返回未定义。我试过 connect-multiparty、body-parser 和 express-fileupload。如何使用 express-fileupload 而不是 multer 使其工作?

这是我的前端:

<form action="/api/upload" method="post">
    <label for="imgHere">file:</label>
    <input type="file" id="imgHere" name="imgHere"><br><br>
    <input type="submit">
</form>

我已经使用检查元素网络选项卡进行了检查,它可以正常发送图像。

这是我的后端:

const express = require("express");
const app = express();
const fileUpload = require("express-fileupload")

app.use(fileUpload())

app.post('/api/upload', function(req, res) {
  console.log(req.files) // logs undefined
  res.send("uploaded.")
});

app.listen(80, function()
{
    console.log("Server loaded.")
});

我该如何解决这个问题?

【问题讨论】:

    标签: javascript node.js express express-fileupload


    【解决方案1】:

    您还需要指定正确的enctype 作为表单属性,如下所示:

    <form action="/api/upload" method="post" enctype="multipart/form-data">
        <label for="imgHere">file:</label>
        <input type="file" id="imgHere" name="imgHere"><br><br>
        <input type="submit">
    </form>
    

    【讨论】:

      猜你喜欢
      • 2018-08-19
      • 2019-07-18
      • 2023-03-11
      • 1970-01-01
      • 2021-08-29
      • 1970-01-01
      • 2014-04-21
      • 2022-01-22
      • 1970-01-01
      相关资源
      最近更新 更多