[英]File upload using Express and multer
I can't seem to get multer to work no matter what I do; 无论做什么,我似乎都无法工作。 I've read everything I could find on the matter, and as far as examples go--I've followed them to the best of my ability.
我已经阅读了有关此事的所有内容,并且就示例而言,我已竭尽所能。
Here is a JSFiddle link to my work . 这是我工作的JSFiddle链接 。
I can never seem to get the file to upload, despite it actually uploading. 尽管实际上已经上传了文件,但我似乎永远都无法上传它。 In my code, I have
在我的代码中,我有
app.post('/submit', function(req, res) {
console.log("____ Clicked submit ____");
console.log(req.body);
console.dir(req.files);
});
The req.body.attachment
will show the filename I attached. req.body.attachment
将显示我附加的文件名。 However, req.files
returns undefined
. 但是,
req.files
返回undefined
。
I appreciate any and all help. 我感谢所有帮助。
You have to use the multer
handler somewhere in your middleware chain. 您必须在中间件链中的某处使用
multer
处理程序。 You have defined the handler with: 您已使用以下方法定义了处理程序:
var upload = multer({
dest: './tmp/'
});
But, you do not use it in your middleware chain. 但是,您不要在中间件链中使用它。
There are two options: 1) Use a global handler (top of the chain, not recommended) 2) Use it on your submit
route (recommended) 有两个选项:1)使用全局处理程序(不建议使用链的顶部)2)在
submit
路径上使用它(推荐)
Read the docs for specific implementation, but the following should work. 阅读文档以了解具体的实现方式,但以下步骤应该可以使用。
// Multer parses single field "attachment"
app.post('/submit', upload.single('attachment'), function(req, res) {
console.log("____ Clicked submit ____");
console.log(req.body);
// console.dir(req.files);
// for single field use `req.file`, not `req.files`
console.log(req.file);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.