简体   繁体   English

如何在javascript中创建txt文件

[英]how to create txt file in javascript

 if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); } else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.open("GET","t1.txt",false); xmlhttp.send(); xmlDoc=xmlhttp.responseText; xmlhttp.open("w","t1.txt"); xmlhttp.writeln('hai'); xmlhttp.close(); console.log(xmlDoc); 

I'll read t1.txt file using XMLHttpRequest() , How I'll write some more text in same file for t1.txt 我将使用XMLHttpRequest()读取t1.txt文件,如何在t1.txt的同一文件中写入更多文本

You can't. 你不能。 Browser-side javascript are not allowed to write to client machine without disabling a lot of security options. 在不禁用大量安全选项的情况下,不允许浏览器端javascript写入客户端计算机。

Instead, you could download a new file using this code 相反,您可以使用此代码下载新文件

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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM