multer - req.file总是未定义 [英] multer - req.file always undefined
本文介绍了multer - req.file总是未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经看到了同样的问题的很多答案,但是我还没有找到一个有效的解决方案。我试图制作一个网络应用程序,您可以将文件上传到使用express和multer,而且我有一个问题,即无法上传文件,并且req.file始终未定义。
I've looked at a lot of answer for this same question, but I haven't found a working solution yet. I am trying to make a web app that you can upload files to using express and multer, and I am having a problem that no files are being uploaded and req.file is always undefined.
我的代码如下
'use strict';
var express = require('express');
var path = require('path');
var multer = require('multer')
var upload = multer({ dest: 'uploads/' })
var app = express();
require('dotenv').load();
app.use(express.static(path.join(__dirname, 'main')));
app.post('/upload', upload.single('upl'), function (req, res, next) {
// req.file is the `avatar` file
// req.body will hold the text fields, if there were any
console.log(req.file);
res.status(204).end();
})
var port = process.env.PORT || 8080;
app.listen(port, function () {
console.log('Node.js listening on port ' + port + '...');
});
表单
<form class="uploadForm" action="/upload" method="post" enctype="multipart/formdata">
<label class="control-label">Select File</label>
<input name="upl" id="input-1" type="file" class="file">
<input type="submit" value="submit" />
</form>
帮助非常感谢,这让我疯狂了。
Help very much appreciated, this is driving me crazy.
推荐答案
您的 enctype
略有不正确,应该是 multipart / form-data
而不是
multipart / formdata
。
Your enctype
is slightly incorrect, it should be multipart/form-data
instead of multipart/formdata
.
这篇关于multer - req.file总是未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文