[removed] Download data to file from content within the page

后端 未结 8 1528
时光说笑
时光说笑 2020-12-13 14:35

setting is the following: I have a homepage where I display a diagram that has been constructed using comma seperated values from within the page. I\'d like to give users th

相关标签:
8条回答
  • 2020-12-13 15:02

    The most comprehensive solution I have run across is using FileSaver.js, handling many potential cross-browser issues for you with fallbacks.

    It takes advantage of the Blob object as other posters have done, and the creator even creates a Blob.js polyfill in case you need to support browser versions that don't support Blob

    0 讨论(0)
  • 2020-12-13 15:04

    Tested on Safari 5, Firefox 6, Opera 12, Chrome 26.

    <a id='a'>Download CSV</a>
    <script>
        var csv = 'a,b,c\n1,2,3\n';
        var a = document.getElementById('a');
        a.href='data:text/csv;base64,' + btoa(csv);
    </script>
    

    HTML5

    <a id='a' download='Download.csv' type='text/csv'>Download CSV</a>
    <script>
        var csv = 'a,b,c\n1,2,3\n';
        var data = new Blob([csv]);
        var a = document.getElementById('a');
        a.href = URL.createObjectURL(data);
    </script>
    
    0 讨论(0)
提交回复
热议问题