问题
I've been trying for days to upload an image using Multer. This is how far I got. I tried multiple things, and can't seem to make it work. I don't know how to use Multer to upload an image in my createUser function in the format shown below.
My ' server.js ' file:
var express = require('express');
var multer = require('multer');
var upload = multer({dest: 'uploads/'});
var router = require('./app/routes');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var session = require('client-sessions');
var DB_URI = "mongodb://localhost:27017/portfolio";
var app = express();
var path = require('path');
var fs = require('fs');
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended:false}));
app.use(express.static(__dirname+ '/public'));
app.use(session({
cookieName: 'session',
secret: 'random_string_goes_here',
duration: 30 * 60 * 1000,
activeDuration: 5 * 60 * 1000,
}));
mongoose.connect(DB_URI);
app.use(router);
app.listen(8080, function(){
console.log("server is listening on port 8080");
})
My ' routes.js '
var express = require('express');
var router = express.Router();
var projectController = require('./controllers/projectController');
var userController = require('./controllers/userController');
router.post('/regUser', userController.createUser);
My ' index.ejs '
<form class="form inv" method="POST" action="/regUser" id="reg_form">
<h3 style="margin-bottom:40px;">Register</h3>
<!-- <input type="file" name="file"/> -->
<input type="text" class="form-control" name="name" placeholder="Name"/>
<input type="text" class="form-control" name="username" placeholder="Username"/>
<input type="text" class="form-control" name="email" placeholder="Email"/>
<input type="password" class="form-control" name="password" placeholder="Password"/>
<input type="file" name="userPhoto"/>
<input type="submit" class="btn btn-default" value="Submit"/>
</form>
My userController.js
createUser: function(req, res){
//add code to create user AND upload image
}
回答1:
There are two problems.
First : your form does not have the enctype attribute set. enctype="multipart/form-data"
is required for file submission to work.
Second : you require multer
, but you are not using it. Add this
app.use(multer({ dest: '/tmp/'}).single('userPhoto'))
(or with another method (see documentation), like .array()
or .fields()
)
You could then access the file in createUser()
like this:
createUser: function(req, res){
console.log(req.file); // this displays the userPhoto's properties
fs.readFile(req.file.path, function (err, data) {
// do something with the file data
}
}
回答2:
I know this has been posted two months ago, but to those who is experiencing the same problem,
I think you must add enctype="multipart/form-data" to your form
<form action="url" method="post" enctype="multipart/form-data">
and in your router.route you must add upload.single() or upload.any()
router.post('/regUser', upload.single(), userController.createUser);
来源:https://stackoverflow.com/questions/42578284/multer-image-upload-in-nodejs-and-express