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

查看:47
本文介绍了如何使用 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

window.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() ?

提前致谢,

编辑

只是为了说明我不存储任意或不需要的数据.所有用户都是系统的注册用户."在正常情况下,我不会在本地存储任何东西.但是,如果在提交表单时网络不可用,我想存储一些 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天全站免登陆