# JS 创建并下载文本文件

# 1. 介绍

create a file and generate a download with Javascript in the Browser (without a server)

# 2. 使用

function download(filename, text) {
    var element = document.createElement('a');
    element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
    element.setAttribute('download', filename);

    element.style.display = 'none';
    document.body.appendChild(element);

    element.click();

    document.body.removeChild(element);
}

# 3. 示例

# 4. 参考

本章目录