如何在javascript中创建txt文件 [英] how to create txt file in javascript

查看:108
本文介绍了如何在javascript中创建txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我将使用 XMLHttpRequest()读取t1.txt文件,如何在t1.txt的同一文件中写入更多文本

I'll read t1.txt file using XMLHttpRequest(), How I'll write some more text in same file for t1.txt

推荐答案

您不能.在不禁用许多安全性选项的情况下,不允许浏览器端的javascript写入客户端计算机.

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

这篇关于如何在javascript中创建txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆