1
옥 양식을 사용하여 이미지를 업로드하고 싶습니다. 게시물 페이지에 표시하고 싶습니다. 이미지가 확장자가없는 파일로 업로드 된 다음 읽을 수 없습니다. 읽을 수 없습니다. 정의되지 않은멀터를 사용하여 이미지를 업로드 할 수 없습니다.
내 app.js 코드의 특성 'mainimage은'내 posts.js 그런
var storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, './public/images/uploads/');
},
filename: function (req, file, callback) {
callback(null, file.fieldname + '-' + Date.now());
}
});
var upload = multer({storage: storage}).single('mainimage');
이하
router.post('/add', function(req, res, nect){
// Get The Form Values
var title = req.body.title;
var category = req.body.category;
var body = req.body.body;
var author = req.body.author;
var date = new Date();
if(req.file.mainimage){
var mainImageOriginalName = req.file.mainimage.originalname;
var mainImageName = req.file.mainimage.name;
var mainImageMime = req.file.mainimage.mimetype;
var mainImagePath = req.file.mainimage.path;
var mainImageExt = req.file.mainimage.extension;
var mainImageSize = req.file.mainimage.size;
} else{
var mainImageName = 'noimage.png';
}
//Form Validation
req.checkBody('title', 'Title field is required').notEmpty();
req.checkBody('body', 'Body field is required');
//Check errors
var errors = req.validationErrors();
if(errors){
res.render('addpost',{
"errors": errors,
"title": title,
"body": body
});
} else{
var posts = db.get('posts');
//submit to db
posts.insert({
"title": title,
"body": body,
"category": category,
"date": date,
"author": author,
"mainimage": mainImageName
}, function(err, post){
if(err){
res.send('There was an issue submitting the post');
} else{
req.flash('success', 'Post Submitted');
res.location('/');
res.redirect('/');
}
});
}
});
그리고 내가 그것을
each post, i in posts
.posts
h1
a(href='/posts/show/#{post._id}')
=post.title
p.meta Posted in #{post.category} by #{post.author} on #{moment(post.date).format('MM-DD-YYYY')}
img(src='/images/uploads/#{post.mainimage}')
!=post.body
a.more(href='/posts/show/#{post._id}') Read More
나는 당신이 나에게 무엇을 시도했지만 여전히 나에게 ** – Horizon
이 posts.js이 라인에서 나에게 오류를 알려줍니다 정의의 ** 속성을 읽을 수 없습니다 'mainimage를'제공 27 '경우 (req.file.mainimage) { var mainImageOriginalName = req.file.mainimage.originalname; var mainImageName = req.file.mainimage.name; var mainImageMime = req.file.mainimage.mimetype; var mainImagePath = req.file.mainimage.path; var mainImageExt = req.file.mainimage.extension; var mainImageSize = req.file.mainimage.size; } else { var mainImageName = 'noimage.png'; }' – Horizon
줄 번호가 없지만'storage' 메소드 다음에'post' 메소드에'req.body.mainImage'에서 이름을 가져올 수 있습니다. – Tom