multer - req.file 总是未定义 [英] multer - req.file always undefined

查看:40
本文介绍了multer - req.file 总是未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于同一个问题,我已经查看了很多答案,但我还没有找到可行的解决方案.我正在尝试制作一个网络应用程序,您可以使用 express 和 multer 上传文件,但我遇到了一个问题,即没有上传文件并且 req.file 始终未定义.

I've looked at a lot of answer for this same question, but I haven't found a working solution yet. I am trying to make a web app that you can upload files to using express and multer, and I am having a problem that no files are being uploaded and req.file is always undefined.

我的代码如下

'use strict';

var express = require('express');
var path    = require('path');
var multer  = require('multer')
var upload  = multer({ dest: 'uploads/' })

var app = express();
require('dotenv').load();

app.use(express.static(path.join(__dirname, 'main')));

app.post('/upload', upload.single('upl'), function (req, res, next) {
  // req.file is the `avatar` file
  // req.body will hold the text fields, if there were any
  console.log(req.file);
  res.status(204).end();
})

var port = process.env.PORT || 8080;
app.listen(port,  function () {
    console.log('Node.js listening on port ' + port + '...');
});

表格

    <form class="uploadForm" action="/upload" method="post" enctype="multipart/formdata">
        <label class="control-label">Select File</label>
        <input name="upl" id="input-1" type="file" class="file">
        <input type="submit" value="submit" />
    </form>

非常感谢帮助,这让我发疯.

Help very much appreciated, this is driving me crazy.

推荐答案

你的 enctype 有点不对,应该是 multipart/form-data 而不是 多部分/表单数据.

Your enctype is slightly incorrect, it should be multipart/form-data instead of multipart/formdata.

这篇关于multer - req.file 总是未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆