如何在javascript中创建txt文件

如何在javascript中创建txt文件,javascript,Javascript,if(window.XMLHttpRequest) { xmlhttp=新的XMLHttpRequest(); } 其他的 { xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”); } open(“GET”,“t1.txt”,false); xmlhttp.send(); xmlDoc=xmlhttp.responseText; open(“w”,“t1.txt”); xmlhttp.writeln('hai'); xmlhttp.close(); cons

if(window.XMLHttpRequest)
{
xmlhttp=新的XMLHttpRequest();
}
其他的
{
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
open(“GET”,“t1.txt”,false);
xmlhttp.send();
xmlDoc=xmlhttp.responseText;
open(“w”,“t1.txt”);
xmlhttp.writeln('hai');
xmlhttp.close();

console.log(xmlDoc)您不能。浏览器端javascript不允许在不禁用许多安全选项的情况下写入客户端计算机

相反,您可以使用此代码下载新文件

function downloadContent(name, content) {
  var atag = document.createElement("a");
  var file = new Blob([content], {type: 'text/plain'});
  atag.href = URL.createObjectURL(file);
  atag.download = name;
  atag.click();
}

downloadContent("t1.txt","hello world");
看见