内容写入txt文件并下载
看代码:
//filename下载文本的名字,text要写入的内容 download(filename, text) { let pom = document.createElement("a"); //获取点击的a标签 filename = this.settings.user.name + '_' + filename + '_' + moment().format('YYYY-MM-DD HH:mm:ss') + '.txt'; 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 { //兼容IE pom.click(); } }