从文件夹中获取所有图像及其相对路径

时间:2021-06-10 17:41:04

标签: node.js

我在根级别的 images/source 目录中有几个图像。我想读取文件夹中的所有图像并获取其路径,如下所示:

var images = ['images/source/first.jpg', 'images/source/second.png', 'images/source/third.png'];

我正在尝试读取类似于下面的目录,但它不起作用!

const imgPath= fs.readdirSync(path.join(__dirname, '/images/source')).sort();

上面的代码读取所有图像,但我需要所有图像及其路径在一个数组中。

1 个答案:

答案 0 :(得分:0)

阅读此论坛。可能会有帮助。

How do you get a list of the names of all files present in a directory in Node.js?

const testFolder = './tests/';
const fs = require('fs');

fs.readdir(testFolder, (err, files) => {
  files.forEach(file => {
    console.log(file);
  });
});
相关问题