最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Edit file - pure js - Stack Overflow

matteradmin1PV0评论

How do I edit a file in pure js (without node)? I get a file with an input field and I read its text like this:

var fileReader = new FileReader();
fileReader.readAsText(file);

fileReader.onload = function () {
    alert(this.result);
}

So, pretty straight forward. I tried to look on the net how to use a fileWriter but with no success. I just need to edit the text inside that file and save it, how can I do?

How do I edit a file in pure js (without node)? I get a file with an input field and I read its text like this:

var fileReader = new FileReader();
fileReader.readAsText(file);

fileReader.onload = function () {
    alert(this.result);
}

So, pretty straight forward. I tried to look on the net how to use a fileWriter but with no success. I just need to edit the text inside that file and save it, how can I do?

Share asked Jan 26, 2018 at 20:43 JohnsonJohnson 3062 gold badges5 silver badges18 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

You can read in the data in the text file, modify it in client side JavaScript (no Node), and then output and re-save it. It does require user interaction, though.

This is a JS fiddle I modified that outputs the file from some text, although it does no reading of files.

Originally taken from this Stackoverflow question

(function () {
    var textFile = null;
  function makeTextFile(text) {
    var data = new Blob([text], {type: 'text/plain'});

    // If we are replacing a previously generated file we need to
    // manually revoke the object URL to avoid memory leaks.
    if (textFile !== null) {
      window.URL.revokeObjectURL(textFile);
    }

    textFile = window.URL.createObjectURL(data);

    return textFile;
  }


  var create = document.getElementById('create');
  var textbox = document.getElementById('textbox');

    //create a click event listener
  create.addEventListener('click', function () {
    var link = document.getElementById('downloadlink');
    link.setAttribute('download', 'info.txt');
    //make the text file
    link.href = makeTextFile(textbox.value);
    link.style.display = 'block';
        //wait for the link to be rendered and then initiate a click to download the file
     window.requestAnimationFrame(function () {
      var event = new MouseEvent('click');
      link.dispatchEvent(event);
      document.body.removeChild(link);
    });
  }, false);

})();

You can't write files in a browser, you'll have to use node.

Post a comment

comment list (0)

  1. No comments so far