【问题标题】:Cannot read property 'fileSize' of undefined (req.file) (multer)无法读取未定义(req.file)(multer)的属性“fileSize”
【发布时间】:2017-04-08 15:28:54
【问题描述】:

代码:

var upload = multer({dest:"./uploads"});

app.post("/", upload.single("file"), function (req, res, next) {
    res.send(req.file.fileSize+"bytes");                     
});

EJS 文件:

    <h1>Get the File Size of your Upload !</h1>

<form enctype="multipart/form-data" method="post" action="/" name="file">
    <input type="file" class="form-control">
    <button type = "submit" class = "btn btn-default">Submit</button>
</form>

问题:

我做错了什么?

【问题讨论】:

  • 您是否在req.file 中获取文件?
  • @Pete 如果我将它记录到控制台,它是undefined。所以没有。
  • 您没有将字段名称指定为.single的参数
  • 你可以试试pathvar upload = multer({ dest: path.join(__dirname, './uploads') });
  • @Pete 没有改变任何东西

标签: javascript node.js express multer


【解决方案1】:

添加 name = 'file' 输入

<h1>Get the File Size of your Upload !</h1>

<form enctype="multipart/form-data" method="post" action="/">
    <input type="file" class="form-control" name="file">
    <button type = "submit" class = "btn btn-default">Submit</button>
</form>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-06
    • 1970-01-01
    • 2018-08-18
    • 2016-03-01
    • 2021-07-30
    • 2021-03-24
    • 2018-07-22
    • 2021-10-02
    相关资源
    最近更新 更多