内容写入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();
    }
  }

  

  

posted @ 2020-08-21 17:16  Angular踩坑者  阅读(453)  评论(0编辑  收藏  举报