相关文章推荐
追风的花生  ·  Map in React js with ...·  2 月前    · 
温柔的保温杯  ·  How to Use the ...·  2 月前    · 
奔跑的凳子  ·  Loading in Rive Files ...·  2 月前    · 
踢足球的遥控器  ·  React DnD·  2 周前    · 
星星上的苦瓜  ·  代码块 | Docusaurus·  6 天前    · 
低调的沙发  ·  Inno ...·  2 年前    · 
豪气的消防车  ·  什么是 AGPL ...·  2 年前    · 
魁梧的水煮鱼  ·  印度观察 | ...·  2 年前    · 

react读取文件夹和文件

在React中读取文件夹和文件可以使用Node.js的内置fs模块。fs模块提供了读取文件夹和文件的方法,例如:

const fs = require("fs");
// 读取文件夹
fs.readdir("folder_path", (err, files) => {
  if (err) throw err;
  console.log(files);
// 读取文件
fs.readFile("file_path", "utf-8", (err, data) => {
  if (err) throw err;
  console.log(data);

请注意,fs模块是一个服务端模块,不能在浏览器中直接使用。因此,您需要使用Webpack或Browserify等工具将其打包到客户端代码中。

  •