具有数组的多部分/表单数据 [英] Multipart/form-data with arrays
问题描述
我有一个简单的表单:
<form method="post" action="/test">
<input type="hidden" name="arr[]" value="val1">
<input type="hidden" name="arr[]" value="val2">
<input type="hidden" name="arr[]" value="val3">
<input type="submit" value="Submit">
</form>
使用控制器:
//...
server.post('/test', function(req, res) {
res.json(req.body);
});
//...
这将返回正常:
{
arr: [
"val1",
"val2",
"val3"
]
}
但是,当我将enctype更改为multipart / formdata
However, when I change the enctype to multipart/formdata
<form method="post" action="/test" enctype="multipart/form-data">
<input type="hidden" name="arr[]" value="val1">
<input type="hidden" name="arr[]" value="val2">
<input type="hidden" name="arr[]" value="val3">
<input type="submit" value="Submit">
</form>
服务器现在响应:
{
arr[]: "val3"
}
有什么问题?有没有一些我需要的配置?
What's the issue? Is there some kind of configuration I need?
如果你想知道,我也在发送一个文件,为什么我需要 multipart / form-data
。
In case you're wondering, I'm also sending a file, that why I need the multipart/form-data
.
推荐答案
这可能与使用 body-parser (特别是 urlencoded
方法),其中<默认情况下,href =https://github.com/expressjs/body-parser#bodyparserurlencodedoptions =nofollow noreferrer>,适用于媒体类型应用程序/ x-www-form-urlencoded
It's probably related to the use of body-parser (especially the urlencoded
method) which, by default, works on requests with the media-type application/x-www-form-urlencoded
only.
您的主应用程序模块可能有以下行:
Your main application module probably has some lines like these:
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded());
我想,您可以添加以下内容以获取类型为 multipart的请求/ form-data
解析:
I suppose, you could just add the following to have requests of type multipart/form-data
parsed as well:
app.use(bodyParser.urlencoded({
type: 'multipart/form-data'
}));
这篇关于具有数组的多部分/表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!