Using HTML5/JavaScript to generate and save a file

后端 未结 17 1719
有刺的猬
有刺的猬 2020-11-21 07:30

I\'ve been fiddling with WebGL lately, and have gotten a Collada reader working. Problem is it\'s pretty slow (Collada is a very verbose format), so I\'m going to start conv

相关标签:
17条回答
  • 2020-11-21 07:42

    Take a look at Doug Neiner's Downloadify which is a Flash based JavaScript interface to do this.

    Downloadify is a tiny JavaScript + Flash library that enables the generation and saving of files on the fly, in the browser, without server interaction.

    0 讨论(0)
  • 2020-11-21 07:43

    Simple solution for HTML5 ready browsers...

    function download(filename, text) {
        var pom = document.createElement('a');
        pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
        pom.setAttribute('download', filename);
    
        if (document.createEvent) {
            var event = document.createEvent('MouseEvents');
            event.initEvent('click', true, true);
            pom.dispatchEvent(event);
        }
        else {
            pom.click();
        }
    }
    

    Usage

    download('test.txt', 'Hello world!');
    
    0 讨论(0)
  • 2020-11-21 07:44

    try

    let a = document.createElement('a');
    a.href = "data:application/octet-stream,"+encodeURIComponent('"My DATA"');
    a.download = 'myFile.json';
    a.click(); // we not add 'a' to DOM so no need to remove

    If you want to download binary data look here

    Update

    2020.06.14 I upgrade Chrome to 83.0 and above SO snippet stop works (due to sandbox security restrictions) - but JSFiddle version works - here

    0 讨论(0)
  • 2020-11-21 07:45

    When testing the "ahref" method, I found that the web developer tools of Firefox and Chrome gets confused. I needed to restart the debugging after the a.click() was issued. Same happened with the FileSaver (it uses the same ahref method to actually make the saving). To work around it, I created new temporary window, added the element a into that and clicked it there.

        function download_json(dt) {
            var csv = ' var data = ';
            csv += JSON.stringify(dt, null, 3);
    
            var uricontent = 'data:application/octet-stream,' + encodeURI(csv);
    
            var newwin = window.open( "", "_blank" );
            var elem = newwin.document.createElement('a');
            elem.download = "database.js";
            elem.href = uricontent;
            elem.click();
            setTimeout(function(){ newwin.close(); }, 3000);
            
        }
    
    0 讨论(0)
  • 2020-11-21 07:51

    HTML5 defined a window.saveAs(blob, filename) method. It isn't supported by any browser right now. But there is a compatibility library called FileSaver.js that adds this function to most modern browsers (including Internet Explorer 10+). Internet Explorer 10 supports a navigator.msSaveBlob(blob, filename) method (MSDN), which is used in FileSaver.js for Internet Explorer support.

    I wrote a blog posting with more details about this problem.

    0 讨论(0)
  • 2020-11-21 07:55

    I've used FileSaver (https://github.com/eligrey/FileSaver.js) and it works just fine.
    For example, I did this function to export logs displayed on a page.
    You have to pass an array for the instanciation of the Blob, so I just maybe didn't write this the right way, but it works for me.
    Just in case, be careful with the replace: this is the syntax to make this global, otherwise it will only replace the first one he meets.

    exportLogs : function(){
        var array = new Array();
    
        var str = $('#logs').html();
        array[0] = str.replace(/<br>/g, '\n\t');
    
        var blob = new Blob(array, {type: "text/plain;charset=utf-8"});
        saveAs(blob, "example.log");
    }
    
    0 讨论(0)
提交回复
热议问题