Reputation: 307
I am new in NodeJS and I am working on a projet using express, ejs and Multer (handle multipart/form-data) in order to upload a picture (single >> profile picture). I have been trying to display the uploaded file (image) in a view without success. I have all the information regarding the uploaded file (req.file) but I can't do anything with it.
//route.js
app.post('/upload_picture', isLoggedIn, upload.single('file'), function(req, res, next){
res.render('image', {
path: req.file.path
});
});
//image.ejs
<section id="upload">
<img class="uploaded-image" src="<%= path %>" alt="Image name: <%= path %>"/>
</section>
Could somebody help me please.
Best regards, Dona
Upvotes: 0
Views: 3707
Reputation: 307
So here is the solution, it worked for me !
var storage = multer.diskStorage({
destination: function(req, file, cb){
cb(null, path.join(__dirname, PICTURE_FOLDER));
},
filename: function(req, file, cb){
var filename = Date.now();
switch (file.mimetype) {
case 'image/png':
filename = filename + ".png";
break;
case 'image/jpeg':
filename = filename + ".jpeg";
break;
default:
break;
}
cb(null, filename);
}
});
var upload = multer({ storage: storage});
app.post('/upload_picture', isLoggedIn, upload.single('file'), function(req, res, next){
res.render('image', {
path: req.file.path
});
});
Best regards, Dona
Upvotes: 1