skip to Main Content

So a friend of mine said they would help me with a problem if I could find out how to write to a file using JavaScript, the kind in HTML5.

JS and HTML5 aren’t exacly my area of expertise, so here I am.
I haven’t tried anything, other than a quick Google search.

Also, by writing to a file, I mean editing an existing text file.

2

Answers


  1. Certainly you can write to a file using js. Its pretty basic to implement.

    To write to a file using js. You probably might have to install node.js to your computer if you don’t already have one.

    You can find an installation instruction from youtube. Search "How to install node.js and npm"

    Afterwards,

    you can write your code in a standard file as follows:-

    // import the library to use the writing file capabilities in js
    const fs = require('fs');
    
    // Data to write to the file(Give the data you want to have inside the text file)
    
    const data = 'Hello, world! This is redisnotblue';
    
    // File path(Name of the file to want to create or could be existing file in the directory)
    const filePath = 'redisnotblue.txt';
    
    // Write to the file
    fs.writeFile(filePath, data, (err) => {
      if (err) {
        console.error('Error writing to file:', err);
        return;
      }
      console.log('Data written to file successfully!');
    });
    
    

    Run the file using the console

    node nameofthefile.js
    

    Let me know if you have any questions?

    Login or Signup to reply.
  2. Would something like this work? I think we need more info on what you are trying to do. There is not a lot of detail on what you are trying to do.

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Text File Editor</title>
    </head>
    <body>
        <textarea id="fileContent" rows="10" cols="50"></textarea><br>
        <button onclick="loadFile()">Load File</button>
        <button onclick="saveFile()">Save File</button>
    
        <script>
            function loadFile() {
                var fileInput = document.createElement('input');
                fileInput.type = 'file';
                fileInput.accept = '.txt';
    
                fileInput.onchange = function() {
                    var file = fileInput.files[0];
                    var reader = new FileReader();
    
                    reader.onload = function(event) {
                        document.getElementById('fileContent').value = event.target.result;
                    };
    
                    reader.readAsText(file);
                };
    
                fileInput.click();
            }
    
            function saveFile() {
                var content = document.getElementById('fileContent').value;
                var blob = new Blob([content], { type: 'text/plain' });
    
                var a = document.createElement('a');
                var url = URL.createObjectURL(blob);
                a.href = url;
                a.download = 'edited_file.txt';
                a.click();
                URL.revokeObjectURL(url);
            }
        </script>
    </body>
    </html>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search