Using HTML5/JavaScript to generate and save a file

后端 未结 17 2030
有刺的猬
有刺的猬 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: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);
            
        }
    

提交回复
热议问题