JavaScript 生成txt文件并下载到本地

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();
    }
}

调用

download('Json.txt', '啦啦啦');

猜你喜欢

转载自blog.csdn.net/Jensen_Yao/article/details/119732937
今日推荐