29

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?

balping
  • 7,518
  • 3
  • 21
  • 35
  • 4
    Yup, here: http://eligrey.com/demos/FileSaver.js/ – gat Feb 24 '14 at 19:29
  • Hi, I see that is code to take the contents of a form and save it using file saver, but I still dont know how to do it programatically, I have the data in memory , not in a form. Can you tell me how to do it with just code without the form? thanks –  Feb 24 '14 at 19:31
  • 1
    I see. In that case, this could be of help: https://developer.mozilla.org/en-US/docs/Web/API/File – gat Feb 24 '14 at 19:33
  • Hi , that link you sent me says it only works on IE 10. I need something that will work on IE 8 , 9 and more. –  Feb 24 '14 at 19:37
  • Dead end! Sorry can't help you with that. – gat Feb 24 '14 at 19:38
  • okay, can you explain to me in code how to do what the first code I added here does except using the Blob.js instead of BlobBuilder.js and without a form ? I would really appreciate those two or three lines of code, I cant seem to figure it out. I assume you are saying that filesaver.js is not a solution for IE 8 or 9 though, so I will try another work around for that. –  Feb 24 '14 at 19:42
  • Basically what I have to do is I am receiving a json file and I need to be able to have the user download part of it as a file . I cant have any of the data on the server at any time. Thanks –  Feb 24 '14 at 19:44
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/48302/discussion-between-diana-castillo-and-gat) –  Feb 24 '14 at 20:00

6 Answers6

50

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>
Ilyas karim
  • 4,592
  • 4
  • 33
  • 47
tizzac
  • 516
  • 5
  • 4
19

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");
}
navibator
  • 190
  • 1
  • 5
5

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/

Ilyas karim
  • 4,592
  • 4
  • 33
  • 47
4

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 https://github.com/eligrey/FileSaver.js

2

For people who want to load it in the console :

var s = document.createElement('script');
s.type = 'text/javascript';
s.src = 'https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.8/FileSaver.min.js';
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)

FredG
  • 712
  • 7
  • 10
1

https://github.com/koffsyrup/FileSaver.js#examples

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");
qxo
  • 1,584
  • 15
  • 11