2016-10-21 14 views
1

翡翠のフォームを使用して画像をアップロードしたいのですが、投稿ページに表示したいのですが画像が拡張子のないファイルとしてアップロードされています未定義multerを使用して画像をアップロードできません。

マイ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('/'); 
      } 
     }); 
    } 

}); 

そして、この私のposts.jadeフォーム

form(method='post', action='/posts/add', enctype='multipart/form-data') 
     .form-group 
      label Title: 
      input.form-control(name='title', type='text') 
     .form-group 
      label Category 
      select.form-control(name='category') 
       each category, i in categories 
        option(value='#{category.title}') #{category.title} 
     .form-group 
      label Body 
      textarea.form-control(name='body', id='body') 
     .form-group 
      label Main Image: 
      input.form-control(name='mainimage', type='file', id='mainimage') 

そして、ここでは、私が

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 

答えて

0

それを表示したい場所をごstorageにあなたがしなくても、ファイルに名前を付けるために延長問題は、おそらくです拡張子。

このコードを追加するには、storageメソッドにこのコードを挿入します。

また
filename: function (req, file, callback) { 
    var extension = file.mimetype; 
    extension = extension.substring(extension.indexOf("/")+1, extension.length); 
    var filename = file.fieldname + '-' + Date.now() + "." + extension; 
    callback(null, filename); 
} 

、あなたはあなたのディレクトリにマップされていない場合、あなたはこのようなファイル、保存されたフォルダにあなたapp.jsで静的マッピングを挿入することができます。で、その後

app.use('/images', express.static(path.join(__dirname, 'images'))); 

postページでは、http://yourdomain:port/images/<filename.extension>を使用して、URLを介してダウンロードしたファイルに直接アクセスできます。

希望します。

+0

私は未定義の「mainimage」あなたは私に言ったことを試してみましたが、それでも私に を与える**プロパティを読み取ることができません** – Horizon

+0

は私にposts.jsライン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

+0

私はあなたの行番号を持っていませんが、' storage'メソッドの後に 'post'メソッドで、' req.body.mainImage'からこれを得る名前を得ることができます。 – Tom

関連する問題