js 本地保存 json/txt 文件
2021-03-27 10:27
                         标签:use   eve   code   charset   var   file   ini   com   防止    通过a标签的download属性,将数据保存到href属性中,模拟点击事件实现文件下载; href属性中也可以直接防止下载文件的地址; js 本地保存 json/txt 文件 标签:use   eve   code   charset   var   file   ini   com   防止    原文地址:https://www.cnblogs.com/recode-hyh/p/12630590.html    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();
            }
        }
        var data = {
            a:11,
            b:[{a:1,b:3}]
        }
        download("data.txt",JSON.stringify(data));
文章标题:js 本地保存 json/txt 文件
文章链接:http://soscw.com/index.php/essay/68543.html