0

Like the title is saying, I'm trying to download a text file in javascript. Best way would be a .docx file but .txt is ok. I saw the blob framework and tried it but doesn't work.. Looks like it can't save my file.

My code to save my text file:

 function totxt(){
      var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});
      saveAs(blob, "helloworld.txt");
 }
Bahadir Tasdemir
  • 10,325
  • 4
  • 49
  • 61
user5488652
  • 421
  • 3
  • 8
  • 15

1 Answers1

0

You may have forgot to insert needed JS library files. You can find the demo in this link, like this:

function totxt() {
  var blob = new Blob(["Hello, world!"], {
    type: "text/plain;charset=utf-8"
  });
  saveAs(blob, "helloworld.txt");
}

$("#test").on("click", function(e) {
  e.preventDefault();
  totxt();
});
<body>

  <button id="test">press</button>
  <script src="https://cdn.rawgit.com/eligrey/Blob.js/0cef2746414269b16834878a8abc52eb9d53e6bd/Blob.js" />
  <script src="https://cdn.rawgit.com/eligrey/FileSaver.js/e9d941381475b5df8b7d7691013401e171014e89/FileSaver.min.js" />

</body>
Bahadir Tasdemir
  • 10,325
  • 4
  • 49
  • 61