In the latest filesaver documentation, an example is given for how to use filesaver.js in conjunction with blobbuilder.js:

var bb = new BlobBuilder(); bb.append("Lorem ipsum"); var fileSaver = window.saveAs(bb.getBlob(), "test_file"); fileSaver.onwriteend = myOnWriteEnd;` 

However blobbuilder.js has been deprecated and they now say to use blob.js (I can't find the code for blobbuilder.js anyway).

Can anyone give me an example of how to use filesaver together with blob.js?

8

6 Answers

Just as example from github, it works.

<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> 
5

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

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="" type="text/javascript"></script> <script type="text/javascript" src="" ></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:

3

wll it looks like I found the answer, although I havent tested it yet

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

from this page

For people who want to load it in the console :

var s = document.createElement('script'); s.type = 'text/javascript'; s.src = ' document.body.appendChild(s); 

Then :

saveAs(new Blob([data], {type: "application/octet-stream ;charset=utf-8"}), "video.ts") 

File will be save when you're out of a breakpoint (at least on Chrome)

Saving text(All Browsers)

saveTextAs("Hi,This,is,a,CSV,File", "test.csv"); saveTextAs("<div>Hello, world!</div>", "test.html"); 

Saving text(HTML 5)

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy