JS实现TXT文件下载

<script type="text/javascript">
    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("data.txt", "hello word!");  // 调用
</script>

Reference: https://blog.csdn.net/weixin_33995481/article/details/88038261

posted on 2019-06-25 11:00  跬步DYB  阅读(5246)  评论(0编辑  收藏  举报

导航