如何在纯 javascript 中创建和写入临时文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20944802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 19:41:08  来源:igfitidea点击:

how to create and write a temporary file in pure javascript?

javascript

提问by Poles

I have a xml content and I want to write it in a temporary xml file and later prompt user to save it.I have this...

我有一个 xml 内容,我想把它写在一个临时的 xml 文件中,然后提示用户保存它。我有这个...

var xml_content = document.getElementById('content_xml').value;
var downloadable=encodeURIComponent(xml_content);
document.location= 'data:Application/octet-stream,' +downloadable;

please help me with a pure javascript code, I mean without jquery.

请用纯javascript代码帮助我,我的意思是没有jquery。

回答by Edgar Villegas Alvarado

You could use localstorage to achieve something similar

您可以使用 localstorage 来实现类似的功能

localStorage.setItem("tempxml", xml_content);

Then, for retrieving it (even when the user closed and reopened the browser):

然后,为了检索它(即使用户关闭并重新打开浏览器):

var xml_content = localStorage.getItem("tempxml");

localStorageis available in recent browsers, contents are saved on user's browser data (client side of course).

localStorage在最近的浏览器中可用,内容保存在用户的浏览器数据中(当然是客户端)。

Hope this helps. Cheers

希望这可以帮助。干杯