利用a标签来实现保存
function download(filename, text) {
var el = document.createElement('a');
//创建a标签
el.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
//href URL 规定链接指向的页面的 URL。
//encodeURIComponent() 函数可把字符串作为 URI 组件进行编码。
el.setAttribute('download', filename);
//a标签的download属性 download filename 规定被下载的超链接目标。
//以下根据实际情况设置
el.style.display = 'none';
//隐藏
document.body.appendChild(el);
el.click();
//点击一下
document.body.removeChild(el);
//移除元素
}
//下载一个名为1.txt,内容为111的文件
download("1.txt","111")