我正在构建一个用于上传图片的应用程序,当我需要在上传图片时获取日期时我遇到了问题。图像将随机上传,因此日期必须等于上传日期。 我正在使用电子应用程序和图像上传我正在使用multer。
这是我的图片库代码,我将图片上传到图片库。如果有人知道它应该如何,我将非常感谢你的回复。
// Image gallery
app.get('/gallery', (req, res) => {
let images = getImagesFromDir(path.join(__dirname, 'public/uploads'));
let time = new Date(images.lastModified); //here I'm trying to get date
res.render('gallery', { title: 'Galerija slik', images: images, time: time })
});
// dirPath: target image directory
function getImagesFromDir(dirPath) {
// All iamges holder, defalut value is empty
let allImages = [];
// Iterator over the directory
let files = fs.readdirSync(dirPath);
// Iterator over the files and push jpg and png images to allImages array.
for (file of files) {
let fileLocation = path.join(dirPath, file);
var stat = fs.statSync(fileLocation);
if (stat && stat.isDirectory()) {
getImagesFromDir(fileLocation); // process sub directories
} else if (stat && stat.isFile() && ['.jpg', '.png'].indexOf(path.extname(fileLocation)) != -1) {
allImages.push('uploads/'+file); // push all .jpf and .png files to all images
}
}
// return all images in array formate
return allImages;
}
这是我的HTML代码
<div id="content">
<h2 class="text-center"><%= title %></h2>
<div id="filters">
<a href="#" id="newer" class="selected">Newer</a>
<a href="#" id="older">Older</a>
</div>
<div id="posts">
<% for(let image of images) {%>
<article class="post">
<figure>
<a href="<%= image %>" data-fancybox="1"><img src="<%= image %>" /></a>
</figure>
<time><%= time %></time>
</article>
<% } %>
</div>
答案 0 :(得分:0)
我假设您想获得每张图片的最后修改日期。
// Image gallery
app.get('/gallery', (req, res) => {
let images = getImagesFromDir(path.join(__dirname, 'public/uploads'));
res.render('gallery', { title: 'Galerija slik', images: images })
});
// dirPath: target image directory
function getImagesFromDir(dirPath) {
// All iamges holder, defalut value is empty
let allImages = [];
// Iterator over the directory
let files = fs.readdirSync(dirPath);
// Iterator over the files and push jpg and png images to allImages array.
for (file of files) {
let fileLocation = path.join(dirPath, file);
var stat = fs.statSync(fileLocation);
if (stat && stat.isDirectory()) {
getImagesFromDir(fileLocation); // process sub directories
} else if (stat && stat.isFile() && ['.jpg', '.png'].indexOf(path.extname(fileLocation)) != -1) {
allImages.push({path: 'uploads/'+file, lastModifiedDate: stat.mtime}); // push all .jpf and .png files to all images
}
}
// return all images in array formate
return allImages;
}
html应该像下面的那样更新。
<div id="posts"> <% for(let image of images) {%> <article class="post"> <figure> <a href="<%= image.path %>" data-fancybox="1"><img src="<%= image.path %>" /></a> </figure> <time><%= image.lastModifiedDate %></time> </article> <% } %> </div>