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...
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");}
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>