How to use filesaver.js

后端 未结 6 606
南方客
南方客 2020-12-03 07:16

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.app         


        
相关标签:
6条回答
  • 2020-12-03 07:57

    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)

    0 讨论(0)
  • 2020-12-03 07:58

    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: http://www.wapgee.com/story/248/guide-to-create-zip-files-using-javascript-by-using-jszip-library

    0 讨论(0)
  • 2020-12-03 08:05

    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>
    
    0 讨论(0)
  • 2020-12-03 08:06

    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");
    }
    
    0 讨论(0)
  • 2020-12-03 08:08

    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

    0 讨论(0)
  • 2020-12-03 08:11

    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");
    
    0 讨论(0)
提交回复
热议问题