admin管理员组文章数量:1323714
I'm new to javascript and I wanted to know if I can export data into a txt file. I can put the data in an alert but I want it to be downloaded onto the clients system as a txt file. How do I achieve this?
I'm new to javascript and I wanted to know if I can export data into a txt file. I can put the data in an alert but I want it to be downloaded onto the clients system as a txt file. How do I achieve this?
Share Improve this question asked Mar 5, 2013 at 9:47 MisterGeekyMisterGeeky 2541 gold badge8 silver badges20 bronze badges 1- 1 Similar question and answer here: stackoverflow./a/43135989/4533488 – aero Commented Feb 15, 2021 at 19:39
2 Answers
Reset to default 3At the moment the File API: Writer is not ready, so you do not have direct interfaces to save file.
Still, you can create a link and put the text in the url.
var link = document.createElement('a');
link.href = 'data:text/plain;charset=UTF-8,' + encodeURIComponent(yourTextGoesHere);
link.innerHTML = 'Open the text file';
//set default action on link to force download, and set default filename:
link.download = 'some file name.txt';
//now put the link somewhere in the html document:
document.body.appendChild(link);
Written by hand and not tested. Should work, but might require debugging.
Edit:
added download
attribute.
Save text
function save(text){
var link = document.createElement('a');
link.href = 'data:text/plain;charset=UTF-8,' + escape(text);
link.download = 'output.txt';
link.click();
}
本文标签: jqueryJavascript Exporting Data to a txt fileStack Overflow
版权声明:本文标题:jquery - Javascript: Exporting Data to a .txt file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742127361a2422002.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论