在 NodeJS 上同时接收相同名称输入和多部分/表单数据输入作为数组和文件附件

cav*_*llo 5 forms file-upload input node.js express

我在使用enctype 时遇到一些麻烦,multipart/form-data同时发送具有相同名称的输入以作为数组获取。我似乎只能获取上传的图像或数组输入,但不能同时获取两者......

例如,我有这样的表格:

<form method="post" action="/test">
  <input name="testinput" value="valueA">
  <input name="testinput" value="valueB">
  <input type="file" name="fileattachment">
  <input type="submit" value="Submit">
</form>
Run Code Online (Sandbox Code Playgroud)

如果我将表单的 enctype 设置为multipart/form-data,如下所示:

<form method="post" action="/test" enctype="multipart/form-data">
Run Code Online (Sandbox Code Playgroud)

我最终在 NodeJS 应用程序中很好地收到了“文件附件”,但我只获得了“testinput”的最后一个值,如下所示:

//req.body
//---
{
    testinput: 'valueB' // I'm missing valueA!
}

//req.files
//---
{
    fileattachment: { 
        name: 'biglogo.png',
        data: <Buffer 89 ... >,
        encoding: '7bit',
        mimetype: 'image/png',
        mv: [Function] 
    }
}
Run Code Online (Sandbox Code Playgroud)

如果未设置 enctype,“testinput”数据将作为数组出现,但“fileattachment”会丢失,我只能获取上传文件的名称,如下所示:

//req.body
//---
{
    testinput: ['valueA', 'valueB'],
    fileattachment: 'some_picture.png' // Useless for file uploading
}
Run Code Online (Sandbox Code Playgroud)

我认为这与我设置express'主体解析器的方式有关,但我似乎无法找出正确的配置。这是我的设置(针对相关代码进行了简化):

var express = require('express');
var fileUpload = require('express-fileupload');
var bodyParser = require('body-parser');

var app = express();
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use(fileUpload()); // Must be placed after, not before, bodyparser's use, otherwise files fail to be uploaded correctly...

app.post('/test', function(req, res) { 
    // Some code
});
Run Code Online (Sandbox Code Playgroud)

另外,这是我的 package.json 文件:

{
    "name": "my-app",
    ...
    "dependencies": {
        "body-parser": "~1.15",
        "express": "~4.14",
        "express-fileupload": "^0.0.5"
    }
}
Run Code Online (Sandbox Code Playgroud)

这是运行在node/6.9.1

我见过这个非常相似的问题Multipart/form-data with arrays,但它已经有 2 年历史了,没有答案,而且似乎没有使用 dependency fileUpload

另外,我尝试了这个问题Handling input arrays in Express forms?的答案提出的方法。,但我在服务器上看到的只是文本而不是数组,如下所示:

{ 
    'something[0][testinput]': 'valueA',
    'something[1][testinput]': 'valueB'
}
Run Code Online (Sandbox Code Playgroud)

我缺少什么?我应该尝试什么?

cav*_*llo 1

express-fileupload通过切换到多方,我能够获得所需的结果

设置:

var express = require('express');
var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
Run Code Online (Sandbox Code Playgroud)

我的代码:

var multiparty = require('multiparty');
app.post('/test', function(req, res) { 
    (new multiparty.Form()).parse(req, function(err, fields, files) {
        // handling fields and files code
    });
});
Run Code Online (Sandbox Code Playgroud)

领域:

{ 
    testinput: ['valueA', 'valueB']
}
Run Code Online (Sandbox Code Playgroud)

文件:

{ 
    fileattachment: [ 
        { 
            fieldName: 'fileattachment',
            originalFilename: 'biglogo.png',
            path: '/tmp/blablaasdfgh.png',
            headers: [Object],
            size: 10130 
        } 
    ]
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,输入被捆绑在一个数组上,并且文件似乎已正确接收。