如何在Chrome中将localStorage数据写入文本文件 [英] How to write localStorage data to a text file in Chrome
问题描述
我想将一个localStorage项目写入一个文本文件,并希望调用用户将文件存储在指定的位置。请帮我扩展代码
I want to write a localStorage item to a text file and want to invoke user to store the file in a specified location. Please help me with extending the code
var data = JSON.parse(localStorage.getItem(pid));
var Text2Write = "";
for(var pr in ele)
{
var dat = pr + ":" + ele[pr] + "\n";
Text2Write += dat;
}
// Here I want to store this Text2Write to text file and ask user to save where he wants.
请帮我扩展这段代码。
Please help me extending this code.
推荐答案
如果您不想使用服务器端解决方案(当然不一定是PHP),最简单的方法是使用数据URI:
If you don't want to use a server-side solution (does not have to be PHP of course), the easiest way is to use a data URI:
data:text/plain,Your text here
这将在浏览器中显示文本文件,并让用户将其保存在他们想要的位置。我不认为有可能为这些类型的URI显示另存为-dialog:
This will show the text file in the browser, and let the user save it where they want. I don't think it's possible to show a "Save as"-dialog for those kind of URI:s.
注意:这至少需要IE8。但我猜这就是你的要求,因为你正在使用localStorage。
Note: this requires IE8 at least. But I'm guessing that's your requirement anyway, since you are using localStorage.
这篇关于如何在Chrome中将localStorage数据写入文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!