要求的事情.使用multer上传文件时未定义文件
req.files is undefined when uploading file with multer
我正试图在Express.js 4中构建一个Node.js应用程序,该应用程序上传图像。我决定使用multer
模块,但无法通过req.files
访问上传的文件。这是我正在使用的代码。我把它限制在我认为相关的部分。
form(method="POST", action="createPost", enctype="multipart/form-data")
input(type="file", name="photo")
br
input(type="submit" value="upload")
在路线/admin.js: var express = require('express');
var multer = require('multer');
var router = express.Router();
var upload = multer({dest: './uploads/'});
router.post('/createPost', upload.single('photo'), function(req, res, next) {
console.log('files:', req.files);
console.log('body:', req.body);
// more code
}
输出:files: undefined
body: {}
该文件存储在uploads
文件夹中,但我无法在req.files
中访问其信息。有人能帮我吗?
当使用upload.single()
时,根据multer文档,结果文件应该在req.file
中,而不是req.files
。请看他们的文档中的例子。
app.post('/profile', upload.single('avatar'), function (req, res, next) {
// req.file is the `avatar` file
// req.body will hold the text fields, if there were any
})
这里是upload.single()
的实际文档:
.single(字段名)
接受名称为fieldname的单个文件。单文件将是保存在req.file.
相关文章:
- 在不破坏未定义函数的情况下,对多个视图使用单个js文件
- 在页面对象文件中验证sendKeys结果会导致未定义的错误(Protractor)
- 使用index.ts文件导出类会导致注入的构造函数中未定义
- JavaScript未定义的函数外部JavaScript文件
- Gulp uglified文件中未定义角度
- 在CoffeeScript文件上运行Jasmine测试时,对象未定义错误
- 导入的文件未定义
- 浏览并选择用户硬盘驱动器中的文件在IE中未定义
- jQuery文件上传和验证未定义函数
- 未定义节点不是server.js文件中的函数
- Typescirpt:XMLHttpRequest未在.ts文件中定义
- 在Node.js中,从同一文件中的另一个函数引用一个函数会导致'ReferenceError:-函数-未定义
- PHP未定义的索引(由jquery文件上传引起)
- JsLint4Java'未定义'使用多个.js文件时发出警告
- 通过php上传Angular JS文件——未定义的索引:
- TypeError:[].文件未定义.在jquery文件中发布
- 外部JavaScript文件未定义
- 文件未定义;Javascript与IMacros
- Jcrop和IE9图像文件未定义
- FileReader读取文件未定义的结果