js生成txt文件

function download(filename, text) {
  var element = document.createElement('a');
  element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
  element.setAttribute('download', filename);
 
  element.style.display = 'none';
  document.body.appendChild(element);
 
  element.click();
 
  document.body.removeChild(element);
}
 
 download("hello.txt","This is the content of my file :)");
复制代码

转载于:https://juejin.im/post/5d09b0bf6fb9a07ef201245f

你可能感兴趣的:(js生成txt文件)