2013-04-29 43 views
0

我想知道如何查看玉上傳的圖片下面是我的一些示例代碼的下方如何查看玉上傳圖片

上傳圖片

app.post('/add', function (req, res, next) { 
    var person = new Person({ 
    image: './public/images/' + req.files.person.image.name; 
    }); 

    var tmp_path = req.files.person.image.path; 
    var target_path = './public/images/' + req.files.person.image.name; 

    fs.rename(tmp_path, target_path, function (err) { 
    if (err) { 
     return next(new Error(err)); 
    } 

    fs.unlink(tmp_path, function (err) { 
     if (err) { 
     console.log(err); 
     } 

     person.save(function (err) { 
     if (err) { 
      return next(new Error(err.message)); 
     } 

     res.redirect('/view/' + person._id); 
     }); 
    }); 
    }); 
}); 

人查看頁面

app.get('/view/:id', function (req, res, next) { 
    Person.findById(req.params.id, function (err, person) { 
    if (err) { 
     return next(new Error(err)); 
    } 

    if (!person) { 
     return next(new Error('Invalid reference to person information')); 
    } 

    fs.readFile(person.image, 'binary', function (err, file) { 
     if (err) { 
     return next(new Error(err)); 
     } else { 
     res.render(path + 'view', { 
      title: title, 
      person: person, 
      file: file 
     }); 
     } 
    }); 
    }); 
}); 

並在我的view.jade我用來顯示圖片

img(src='#{file}') 

但它沒有顯示,有人請幫助我!

回答

0

治療file作爲括號

img(src=file) 
+0

是這塊玉好模板引擎? – underscore 2013-04-29 16:36:51

+0

我對玉有很好的效果,也是我最常用的。有些人不喜歡的語法,但對我來說這是好的 – Noah 2013-04-29 16:38:47

+0

謝謝Noah.one更多?使用直接html會更快嗎? – underscore 2013-04-29 16:40:00

0

你可以嘗試IMG(SRC = '#{} locals.file')

+0

對於.file文件缺少本地前綴。我已經添加了,並讓我們看看是否有幫助 – V31 2013-05-04 17:32:26

0

此代碼爲我工作,以顯示圖像的內部JavaScript變量玉。

.image-block 
     h2 
     img(src="/image.jpg" alt="image not found") 

img(src="/#{pathTOImage}" alt="image not found") 

而且在哈巴狗你寫這樣的代碼:

img(src="/" + pathTOImage)