【问题标题】:Bodyparser for multipart Nodejs多部分 Nodejs 的 Bodyparser
【发布时间】:2017-02-15 22:00:35
【问题描述】:

所以我想从我的身体解析器开始,而且我正在使用'multer'

我的多重选择:

var multer = require('multer');
var storage = multer.diskStorage({
destination: function (req, file, cb) {
    cb(null, '/root/Unicon-Oauth/Resources/profile_images/')
},
filename: function (req, file, cb) {
    cb(null, file.fieldname + '-' + Date.now())
}
});

var pfImage = multer({storage:storage});

server.js 中的正文解析器

app.use(bodyParser.urlencoded({extended:true,limit: '20MB',parameterLimit:10000}));
app.use(bodyParser.json());

我有这样的路线

router.post('/edit',[auth.isAuthenticated,pfImage.single('pImage')],actions.edit);

功能是这样的

function edit(req,res)
{
  console.log(req.body);
}

控制台日志输出:

块引用

{"-----WebKitFormBoundaryGS8GEzQls8xRP6nt\r\n内容配置:表单数据;名称":"\"_id\"\r\n\r\n58a4735cfa328b7e9eaf6a3a\r\n----- WebKitFormBoundaryGS8GEzQls8xRP6nt\r\nContent-Disposition: form-data; name=\"city\"\r\n\r\nKayseri\r\n------WebKitFormBoundaryGS8GEzQls8xRP6nt\r\nContent-Disposition: form-data; name =\"name\"\r\n\r\nali\r\n------WebKitFormBoundaryGS8GEzQls8xRP6nt\r\nContent-Disposition: form-data; name=\"\"\r\n\r\n \r\n------WebKitFormBoundaryGS8GEzQls8xRP6nt--\r\n"}

如何将其解析为 req.body ?

【问题讨论】:

  • 您是否尝试通过 url 传递图像数据?
  • 不通过邮递员我添加了截图

标签: node.js middleware body-parser


【解决方案1】:

问题是您正在发送多部分/表单数据请求您正在覆盖 Content-Type 并将其设置为 不同 类型(应用程序/x-www-form-urlencoded),这是一种完全不同的格式。如果你去掉这个覆盖,应该没问题。

【讨论】:

    猜你喜欢
    • 2017-04-06
    • 1970-01-01
    • 2017-08-12
    • 2019-05-15
    • 1970-01-01
    • 2016-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多