How to use filesaver.js
It works in my react project:
import FileSaver from 'file-saver';
// ...
onTestSaveFile() {
var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});
FileSaver.saveAs(blob, "hello world.txt");
}
Here is a guide to JSZIP to create ZIP files by JavaScript. To download files you need to have filesaver.js, You can include those libraries by:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.4/jszip.min.js" type="text/javascript"></script>
<script type="text/javascript" src="https://fastcdn.org/FileSaver.js/1.1.20151003/FileSaver.js" ></script>
Now copy this code and this code will download a zip file with a file hello.txt having content Hello World. If everything thing works fine, this will download a file.
<script type="text/javascript">
var zip = new JSZip();
zip.file("Hello.txt", "Hello World\n");
zip.generateAsync({type:"blob"})
.then(function(content) {
// see FileSaver.js
saveAs(content, "file.zip");
});
</script>
Now let's get in to deeper. Create an instance of JSZip.
var zip = new JSZip();
Add a file with a Hello World text:
zip.file("hello.txt", "Hello World\n");
Download the filie with name archive.zip
zip.generateAsync({type:"blob"}).then(function(zip) {
saveAs(zip, "archive.zip");
});
Read More from here: https://www.wapgee.com/post/5/
Just as example from github, it works. https://github.com/eligrey/FileSaver.js
<script src="FileSaver.js"></script>
<script type="text/javascript">
var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});
saveAs(blob, "hello world.txt");
</script>