14

As my title says, is it possible? I am begginer at front-end and trying to teach myself reactJS right now. I learned some javascript before and I know it is impossible to read or write files with it. Mainly what I want to do is to get string from input and lets say write it into file.

3
  • Looks like it's already asked and answered here: stackoverflow.com/questions/36569068/… Commented Mar 26, 2018 at 12:44
  • Yes, but in the link you provided person asks if he can read from file. My question (sorry if I was unclear) was that I want to add text into file from client side (for example: person types in his name and it will save into a file and its gonna be displayed on website permanently until removed manually from the file) Commented Mar 26, 2018 at 12:57
  • This question is similar to: How to create a file in memory for user to download, but not through server?. React is a rendering library, it is orthogonal to writing files. You can do that with client-side JavaScript as per the question above. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Jul 31 at 15:10

3 Answers 3

21

Well the question is, where does that file live?

Node is able to write to files because the files exist on the server that node is running on.

React runs in browser so there is no shared file system to write to. You can read from a file because the contents of that file get bundled into the Javascript that gets served to the browser.

If you want to write to a file, you would need to send an API request from your browser / React to a server and have that server write to the file system.

Additionally, as pointed out by Huy Nguyen, it's possible to write to the clients file system from the browser but that is going to be private to that user.

Sign up to request clarification or add additional context in comments.

2 Comments

So it is only possible using nodeJS combined with react, yes?
Doesn't have to be nodeJS but yes, some form of server side code
7

Your question is a bit vague. In addition to what @Stretch0 said, it's possible to read/write files on a user's computer using the browser's native APIs. Here is a good tutorial.

2 Comments

They hyperlink is redirected to web.dev/read-files which says how to read files not how to write them.
This looks relevant: web.dev/file-system-access
0

React cant directly write into data but we can write files using javascript or node

var fs = require("fs"); 
// example.txt is an empty file 
fs.writeFile( 
  "example.txt", 
  "This is sentences written in a file", 
  function (err) { 
    if (err) { 
      return console.error(err); 
    } 
    
    // Reading the file 
    fs.readFile("example.txt", function (err, data) { 
      if (err) { 
        return console.error(err); 
      } 
      console.log("Data read : " + data.toString()); 
        
    }); 
  } 
); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.