首页 > 解决方案 > Multer 无法在 console.log 中获取单独的字段名称

问题描述

我创建了一个 Express 应用程序以multer在我的 Node.js 应用程序中上传项目。

在我的多上传设置中,我需要分别获取fieldname每个项目的属性。

我得到console.log(req.files);了所有上传的项目,如下面的代码所示。

但我怎么得到fieldname?我想做类似的事情

console.log(req.files.upfile);
console.log(req.files.upfile2);

我希望这里有人知道如何做到这一点。非常感谢帮助,谢谢!

[ { fieldname: 'upfile',
    originalname: 'test_file1.jpg',
    encoding: '7bit',
    mimetype: 'image/jpeg',
    destination: 'storage/',
    filename: 'test_file1.jpg',
    path: 'storage/test_file1.jpg',
    size: 1046949 },
  { fieldname: 'upfile2',
    originalname: 'test_file2.jpg',
    encoding: '7bit',
    mimetype: 'image/jpeg',
    destination: 'storage/',
    filename: 'test_file2.jpg',
    path: 'storage/test_file2.jpg',
    size: 632169 } ]

标签: node.jsexpressnpmmulter

解决方案


当您上传多个文件并使用数组时,您可以在路由器 API 调用中迭代您的请求,如下所示

let files = req.files;
files.forEach((file) => {
    console.log(file.fieldname);
});

推荐阅读