我试图得到一个简单的文件上传机制与Express 4.0工作,但我一直得到未定义的请求。app.post主体中的文件。以下是相关代码:

var bodyParser = require('body-parser');
var methodOverride = require('method-override');
//...
app.use(bodyParser({ uploadDir: path.join(__dirname, 'files'), keepExtensions: true })); 
app.use(methodOverride()); 
//...
app.post('/fileupload', function (req, res) {
  console.log(req.files); 
  res.send('ok'); 
}); 

. .以及附带的Pug代码:

form(name="uploader", action="/fileupload", method="post", enctype="multipart/form-data")
    input(type="file", name="file", id="file")
    input(type="submit", value="Upload")

解决方案 感谢下面mscdex的响应,我已经切换到使用busboy而不是bodyParser:

var fs = require('fs');
var busboy = require('connect-busboy');
//...
app.use(busboy()); 
//...
app.post('/fileupload', function(req, res) {
    var fstream;
    req.pipe(req.busboy);
    req.busboy.on('file', function (fieldname, file, filename) {
        console.log("Uploading: " + filename); 
        fstream = fs.createWriteStream(__dirname + '/files/' + filename);
        file.pipe(fstream);
        fstream.on('close', function () {
            res.redirect('back');
        });
    });
});

当前回答

以下是我在谷歌上找到的:

var fileupload = require("express-fileupload");
app.use(fileupload());

哪个上传机制比较简单

app.post("/upload", function(req, res)
{
    var file;

    if(!req.files)
    {
        res.send("File was not found");
        return;
    }

    file = req.files.FormFieldName;  // here is the field name of the form

    res.send("File Uploaded");


});

其他回答

请使用以下代码

app.use(fileUpload());

问题解决!!!!!!!

事实证明,存储功能甚至没有运行一次。 因为我必须包括app.use(upload)作为upload = multer({storage}).single('file');

 let storage = multer.diskStorage({
        destination: function (req, file, cb) {
            cb(null, './storage')
          },
          filename: function (req, file, cb) {
            console.log(file) // this didn't print anything out so i assumed it was never excuted
            cb(null, file.fieldname + '-' + Date.now())
          }
    });

    const upload = multer({storage}).single('file');

Multer是一个处理“多部分/表单数据”的中间件,神奇地使上传的文件和表单数据以请求的形式提供给我们。文件和request.body。

安装multer:—NPM install multer—save

在.html文件中:-

<form method="post" enctype="multipart/form-data" action="/upload">
    <input type="hidden" name="msgtype" value="2"/>
    <input type="file" name="avatar" />
    <input type="submit" value="Upload" />
</form>

.js文件:-

var express = require('express');
var multer = require('multer');
var app = express();
var server = require('http').createServer(app);
var port = process.env.PORT || 3000;
var upload = multer({ dest: 'uploads/' });

app.use(function (req, res, next) {
  console.log(req.files); // JSON Object
  next();
});

server.listen(port, function () {
  console.log('Server successfully running at:-', port);
});

app.get('/', function(req, res) {
  res.sendFile(__dirname + '/public/file-upload.html');
})

app.post('/upload', upload.single('avatar'),  function(req, res) {
  console.log(req.files); // JSON Object
});

希望这能有所帮助!

Express-fileupload看起来是目前唯一还能工作的中间件。

在同一个例子中,multer和connect-multiparty给出了一个未定义的req值。文件或请求。文件,但express-fileupload工作。

关于req.file/req.files的空值有很多问题。

只是为了补充上面的答案,你可以简化express-fileupload的使用,只需要一个需要它的路由,而不是将它添加到每个路由。

let fileupload = require("express-fileupload");

...

//Make sure to call fileUpload to get the true handler
app.post("/upload", fileupload(), function(req, res){

...

});