node.js - 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 } ]
解决方案
当您上传多个文件并使用数组时,您可以在路由器 API 调用中迭代您的请求,如下所示
let files = req.files;
files.forEach((file) => {
console.log(file.fieldname);
});
推荐阅读
- javascript - 如何从 daterangepicker 中获取选定的值?
- mysql - 如何在 SQL 中按另一个标志过滤
- r - Mapview 重置按钮和将地图居中到初始位置
- jquery - 基于表单中的“是/否”问题,我想更新 Webflow 中现有集合中的一个字段
- java - maven - 将传递依赖设为默认并覆盖直接依赖
- c++ - 如何在 C++ 中比较向量中的字符串?
- python - 如何在不创建新数组的情况下合并 numpy 数组
- mongodb - 在 Mongoose 中,我有具有一对多关系的用户和角色模式。如何查询特定用户是否具有“管理员”角色?
- javascript - 单词翻译动画 VUE 2 CLI + I18N
- python - Azure SDK for Python whoami 调用以获取当前登录的用户