如何使用JavaScript在客户端保存文件? [英] How to Save a file at client side using JavaScript?

查看:124
本文介绍了如何使用JavaScript在客户端保存文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将JSON对象保存到客户端的文件中以保留它以供将来使用,我尝试了以下行

I want to save a JSON object to a file at client to persist it for future usage, I have tried following line

窗口。 open(data:text / json; charset = utf-8,+ escape(JSON.stringify(obj)));

它的工作原理!!问题是它询问用户保存文件的位置和名称。我想要的是我希望用户完全没有意识到某些东西正在保存以备将来使用,或至少保持最小的用户点击次数。

and it works!! Problem it asks user the location and name of the file for saving. What I want is I want the user completely unaware of the fact that something is being saved for future use or atleast keep it to minimum possible user clicks.

我该如何给予中的文件名和位置静态 window.open()

How can I give the file name and location statically in window.open() ?

谢谢提前,

编辑

只是为了说明我不知道 t存储任意或不需要的数据。所有用户都是系统的注册用户。在正常情况下,我不会在本地存储任何东西。但是,如果在提交表单时网络不可用,我想存储一些JSON对象。

Just to make it clear that " I don't store arbitrary or unwanted data. All the users are registered users of the system." In normal conditions I don't store anything locally. However I want to store some JSON objects if the network was not available at the time of form submission.

一个明显的解决方案是使用cookie。由于用户的浏览器设置可能会意外删除Cookie。我需要一种方法来保持数据,直到网络可用。拥有跨浏览器支持会更好。

One obvious solution will be to use cookies. Since Cookies can be accidentally deleted due to user's browser settings. I need a way to persist the data till the network becomes available. It will be greater to have cross browser support.

推荐答案


我想要跨浏览器的值支持,即使用户更改浏览器,他/她应该能够从他离开的地方继续。我将信息存储在JSON对象中。

I want the values for cross-browser support, even if the user changes the browser he/she should be able to proceed from where he left. I am storing information in JSON objects.

我认为用户不希望Web应用程序与其他浏览器共享信息机器这样。此外,我怀疑许多用户过于频繁地更改浏览器以保证这样的隐私侵入功能。您应该考虑在服务器上存储信息(通过强制用户注册或使用OpenID,Google,Facebook等常用帐户),或者通过设置cookie或使用上述存储技术在客户端存储信息。

I think users don't expect a web application to share information with other browsers on the same machine this way. Also, I doubt many users change their browser too frequently anyway to warrant a privacy-invasive feature like this. You should either consider storing the information on your server (by forcing a user to register or using common accounts like OpenID, Google, Facebook etc.) or on the client side by setting a cookie or using the mentioned storage technologies.

如果你真的想将存储的信息限制在同一台机器上的浏览器上,并且不想允许同一用户在不同的机器上访问,你可以看看 LSO ('Flash cookies')似乎与浏览器无关。您不需要任何用户确认来存储LSO。

If you really want to restrict stored information to browsers on the same machine, and don't want to permit access by the same user on different machines, you could take a look into LSO ('Flash cookies') which seem to be saved browser independent. You don't need any user confirmation for storing LSOs.

更新:浏览器不支持Flash,因为它以前并没有广泛使用,所以LSO不是不再是一个不错的选择。

Update: Flash isn't supported by browsers as it used to be and not widely used nowadays, so LSOs aren't a good option any more.

这篇关于如何使用JavaScript在客户端保存文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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