Helo, i am having trouble inserting some data and uploading an image to the server using express and mongoDB.
The problem that i noticed is the enctype='multipart/form-data' of the form. If i put the enctype='multipart/form-data' the image is being uploaded but the data becomes undefined.
If i delete the enctype from the form i cannot upload the image.
Has someone had this problem? or what's the strategy how can i do it different?
form :
<form class='form' method='POST' action='/new' >
<input type="textarea" name='description' class="form-control" placeholder="short description">
<input type="file" name='image' aria-describedby="basic-addon1">
<input type="submit" class="btn btn-primary" aria-describedby="basic-addon1">
</form>
and the middleware:
middleware.addPackage = function(req, res, next){
var storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, 'public/img/');
},
filename: function(req, file, cb) {
cb(null, file.originalname);
}
});
var upload = multer({ storage: storage }).single('image');
// getting the data from the form
var newPackage = {
title : req.body.title,
shortDescription : req.body.description,
author : {
id : req.user._id,
username : req.user.username
},
};
Package.create(newPackage, function(err, addedPackage){
if(err){
console.log('There was a problem creating the new package middleware.addPackage');
console.log('ERROR IS ' + err);
res.redirect('back');
} else {
upload(req, res, function (err) {
if (err) {
return;
}
res.redirect('/');
});
};
});
}
via Adrian
No comments:
Post a Comment