vue 读取文件夹txt中的内容
可以使用Node.js的fs模块来读取文件夹中的txt文件内容。具体步骤如下:
- 引入fs模块
const fs = require('fs');
- 使用fs.readdirSync()方法获取文件夹中的所有文件名
const folderPath = 'path/to/folder';
const files = fs.readdirSync(folderPath);
- 遍历文件名数组,使用fs.readFileSync()方法读取每个txt文件的内容并存储到一个数组中
const txtContents = [];
for (let i = 0; i < files.length; i++) {
const fileName = files[i];
if (fileName.endsWith('.txt')) {
const filePath = folderPath + '/' + fileName;
const content = fs.readFileSync(filePath, 'utf-8');
txtContents.push(content);
}
}
- 最终得到的txtContents数组中存储了文件夹中所有txt文件的内容,可以在Vue组件中使用该数组进行展示。
注意:以上代码中的路径需要根据实际情况进行修改。
原文地址: https://www.cveoy.top/t/topic/bz1l 著作权归作者所有。请勿转载和采集!