最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

how to create txt file in javascript - Stack Overflow

programmeradmin2浏览0评论

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

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

Share Improve this question edited Feb 22, 2016 at 7:33 Shaffiulla Khan 10.4k16 gold badges57 silver badges54 bronze badges asked Feb 22, 2016 at 7:18 vijayvijay 3063 silver badges12 bronze badges 1
  • See stackoverflow.com/questions/30563157/… – guest271314 Commented Feb 22, 2016 at 7:30
Add a comment  | 

1 Answer 1

Reset to default 25

You can't. Browser-side javascript are not allowed to write to client machine without disabling a lot of security options.

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");
发布评论

评论列表(0)

  1. 暂无评论