最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Read file from directory reactjs - Stack Overflow

programmeradmin0浏览0评论

I have a dictionary text file in the project folder and I would like to read it and put items in an array in my App.js file. How can I do that?

Here is how the project looks: Here

I have a dictionary text file in the project folder and I would like to read it and put items in an array in my App.js file. How can I do that?

Here is how the project looks: Here

Share asked Mar 19, 2022 at 18:06 aleksandar3aleksandar3 571 silver badge6 bronze badges 5
  • To be able to help I think more information is required. Is it an electron app or a browser app? When tloading the file will it be a user specified file or will it always be that static file? Are you wanting to read it at run time or are you bundling your app and want it to be read in then? – Mike Lowen Commented Mar 19, 2022 at 18:11
  • stackoverflow./questions/50539756/… – Andy Commented Mar 19, 2022 at 18:11
  • By "items" do you mean things on separate lines? – Andy Commented Mar 19, 2022 at 18:15
  • 1 Questions should have all required information not as external links, but in the question. Also, source code and directory listings will be as formatted text, not screenshots (use backticks/triple backticks as needed). You have not provided any detail on the input data format or any example code, leaving everyone to guess. – JGurtz Commented Mar 19, 2022 at 20:01
  • It is a browser app. It will always be that static file and the words are seperated by \n. I want to place them in array. – aleksandar3 Commented Mar 20, 2022 at 9:30
Add a ment  | 

1 Answer 1

Reset to default 3

First import the text file.

 import dir from './dir.txt';

then you can fetch and transform it into text:

fetch(dir)
  .then(row => row.text())
  .then(text => {
    console.log('text:', text);
  });
发布评论

评论列表(0)

  1. 暂无评论