How to Use Filesaver. Js to Save Text Files

I downloaded the FilSaver.js on github and tried to test it for future purposes.I made a simple html file that accepts an input and when a button is clicked a function is triggered and that function will save the file using the FileSaver.js.

Insinde the folder ofI only have the FileSaver.js and the HTML file.

here is my code.

<html>
    <head>

    </head>
    <body>
        <input type="text" id="sample">
        <button id="button" onclick="download();">save as</button>

        <script src="FileSaver.js"></script>
        <script type="text/javascript">
            function download(){
            var save = document.getElementById("sample").value;
            alert(save);    
            var blob = new Blob([save], {type: "text/plain;charset=utf-8"});
            saveAs(blob, "hello world.txt");}
        </script>
    </body>
</html>

error:

-Uncaught SyntaxError: Unexpected token export on this line of code inside the FileSaver.js var saveAs = saveAs || (function(view) {

-lol.html:15 Uncaught ReferenceError: saveAs is not defined on this line of code inside the html file saveAs(blob, "hello world.txt");}

10

1 Answer

Just update to the following and it will work, try the CDN server used for the filesaver in the below demo

function download() {
  var save = document.getElementById("sample").value;
  var blob = new Blob([save], {
    type: "text/plain;charset=utf-8"
  });
  saveAs(blob, "sample-file.txt");
}
<script src=""></script>
<script src=""></script>
<input type="text" id="sample">
<button id="button" onclick="download();">save as</button>
1

Your Answer

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

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest