Skip to content

Commit 05912c0

Browse files
committed
image upload feature added
Signed-off-by: TG1999 <[email protected]>
1 parent bb3f444 commit 05912c0

File tree

10 files changed

+155
-10
lines changed

10 files changed

+155
-10
lines changed

controller/profile.controller.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@ const user = require('../schema/user.js');
22
const proj = require('../schema/project.js');
33
const expressValidator = require('express-validator');
44
const { check, validationResult } = require('express-validator/check');
5-
5+
// var multer = require('multer');
6+
// var storage = multer.diskStorage({
7+
// destination: (req, file, cb) => {
8+
// cb(null, 'public/images/uploads')
9+
// },
10+
// filename: (req, file, cb) => {
11+
// cb(null, file.fieldname + '-' + Date.now())
12+
// }
13+
// });
14+
// var upload = multer({storage: storage});
615
module.exports = {
716
SubmitprojectForm:function(req,res){
817
res.render('projectForm')
@@ -35,6 +44,7 @@ module.exports = {
3544
]).sort({createdAt:-1}).then((da)=>{
3645
console.log(req.user);
3746
user.findOne({ Eid: req.params.sd }).then(function (use) {
47+
console.log(da)
3848
res.render('other-landing', { use: use, ques: da, sign: req.user});
3949
})
4050
})
@@ -62,7 +72,8 @@ module.exports = {
6272
content: req.body.cont,
6373
upvote: '',
6474
downvote: '',
65-
proid: Math.floor(Math.random() * 100000)
75+
proid: Math.floor(Math.random() * 100000),
76+
image:'/images/uploads/'+req.file.filename
6677
})
6778
.save()
6879
.then(function() {

package-lock.json

Lines changed: 121 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"mongo": "^0.1.0",
2323
"mongodb": "^3.1.0",
2424
"mongoose": "^5.1.7",
25+
"multer": "^1.4.1",
2526
"node": "^10.5.0",
2627
"node-memwatch": "^1.0.1",
2728
"nodemon": "^1.18.8",

routes/profile.routes.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,16 @@ const auth = function(req, res, next) {
1313
next();
1414
}
1515
};
16-
16+
var multer = require('multer');
17+
var storage = multer.diskStorage({
18+
destination: (req, file, cb) => {
19+
cb(null, 'views/images/uploads')
20+
},
21+
filename: (req, file, cb) => {
22+
cb(null, file.fieldname + '-' + Date.now())
23+
}
24+
});
25+
var upload = multer({storage: storage});
1726
route.get('/submitProject',auth,profileController.SubmitprojectForm)
1827
route.get('/search', url, jsonParser, profileController.search);
1928

@@ -23,7 +32,7 @@ route.get('/profile/:id', auth, profileController.profileId);
2332

2433
route.get('/profileview/:sd', auth, url, profileController.profileViewSd);
2534

26-
route.post('/publish', auth, url, profileController.publish);
35+
route.post('/publish', auth, url,upload.single('image'), profileController.publish);
2736

2837
route.post('/upDownVote', auth,url, jsonParser,profileController.upDownVote);
2938

schema/project.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ createdAt:{
4141
endAt:{
4242
type:Date
4343
},
44-
44+
image:{
45+
type:String
46+
}
4547
});
4648

4749
const proj=mongoose.model('project',project);
26.4 KB
Binary file not shown.
26.4 KB
Binary file not shown.
24.2 KB
Binary file not shown.

views/other-landing.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
<!--Text-->
110110
<div class="container">
111111
<div class="row">
112-
112+
<img src='<%=x.image%>' style="max-height:100%;max-width:100%">
113113
<%for(var q=0;q<x.Lang.length;q++){%>
114114
<a href="#" class="btn btn-blue-grey btn-rounded"><%=x.Lang[q]%></a>
115115
<%}%>

views/projectForm.ejs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
<div class="container" id="mainCard">
4949
<div class="card card-body">
5050
<h2 class="card-title">Submit your project</h2>
51-
<form method="POST" action="/profile/publish">
51+
<form method="POST" action="/profile/publish" enctype="multipart/form-data">
5252
<div class="form-group">
5353
<label>Project Name</label>
5454
<input name="contentname" type="type" class="form-control" placeholder="Enter Project Name">
@@ -67,6 +67,10 @@
6767
<div class="form-group">
6868
<textarea name="cont" placeholder="Describe about your project " cols="55" rows="3"></textarea>
6969
</div>
70+
<div class="form-group">
71+
<label>Project Image</label>
72+
<input name="image" type="file" class="form-control" placeholder="Enter Project Image">
73+
</div>
7074
<button type="submit" class="btn btn-primary btn-block">Submit</button>
7175
</form>
7276
</div>

0 commit comments

Comments
 (0)