Save content of textarea as plain txt with .csv extension

前端 未结 1 1622
我在风中等你
我在风中等你 2021-01-15 20:08

Is there a way to save content as a txt file with a csv file extension using HTML5 ?
It shouldn\'t be a real csv.

Some

相关标签:
1条回答
  • 2021-01-15 20:26

    The a tag's new download property can work to your advantage here. More information.

    Proof of concept:

    <!doctype html>
    <html>
        <head>
            <script type="text/javascript">
                function save() {
                    var a = document.createElement('a');
                    with (a) {
                        href='data:text/csv;base64,' + btoa(document.getElementById('csv').value);
                        download='csvfile.csv';
                    }
                    document.body.appendChild(a);
                    a.click();
                    document.body.removeChild(a);
                }
            </script>
        </head>
        <body>
            <textarea id="csv"></textarea><button onclick="save()">Save</button>
        </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题