如何使用 JavaScript 在客户端保存文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8080929/
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-26 02:11:21  来源:igfitidea点击:

How to Save a file at client side using JavaScript?

javascriptjqueryjakarta-ee

提问by Amit

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

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

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

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.

它有效!!问题是询问用户要保存的文件的位置和名称。我想要的是我希望用户完全不知道某些内容正在被保存以备将来使用,或者至少将其保持在尽可能少的用户点击量。

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

我怎么可以给文件名和位置静态window.open()

Thanks in advance,

提前致谢,

EDIT

编辑

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.

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

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.

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

采纳答案by kapex

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.

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

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.

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

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.

如果您真的想将存储的信息限制在同一台机器上的浏览器,并且不想允许同一用户在不同机器上访问,您可以查看LSO('Flash cookies'),它似乎是浏览器保存的独立的。存储 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.

更新:浏览器不支持 Flash,因为它过去和现在没有广泛使用,因此 LSO 不再是一个好的选择。

回答by Quentin

If you want to persist data, then use a storage API, you can't play games with the user's filesystem.

如果你想持久化数据,那么使用存储 API,你不能用用户的文件系统玩游戏。

回答by Gfox

You can't do that, it is a question of security on client side.

你不能这样做,这是客户端的安全问题。

回答by Kamyar Nazeri

FileSystem APIs ispart of HTML5 spec and it is possible to access file system in a sandbox for a certain website in modern browsers, here is a good tutorial: http://www.html5rocks.com/en/tutorials/file/filesystem/

文件系统APIHTML5 规范的一部分,可以在现代浏览器的某个网站的沙箱中访问文件系统,这里有一个很好的教程:http: //www.html5rocks.com/en/tutorials/file/filesystem/

However I would go with LocalStorage API for that matter which has better browser support: http://www.w3schools.com/html5/html5_webstorage.asp

但是,我会使用 LocalStorage API,因为它具有更好的浏览器支持:http: //www.w3schools.com/html5/html5_webstorage.asp

回答by kwh

FSO.jswraps the temporary/persistent FileSystem API and will allow you to easily read/write files... only supported by Chrome right now, but the spec is coming along fast. :)

FSO.js包装了临时/持久文件系统 API,并允许您轻松读/写文件……目前仅受 Chrome 支持,但规范正在快速推出。:)

回答by olly_uk

As has already been mentioned in the comments, this is not possible without express user consent for obvious security reasons. however, as also mentioned in comments, you shoudl/could store the information in a cookie which will then be retrievable when the user returns.

正如评论中已经提到的那样,出于明显的安全原因,未经用户明确同意,这是不可能的。然而,正如评论中所提到的,您应该/可以将信息存储在 cookie 中,然后在用户返回时可以检索该 cookie。

something like this would do it

像这样的事情会做到

document.cookie="cookie_name="+required_value+"; expires=Monday, 04-Dec-2011 05:00:00 GMT";

obviously the expires time will be as long as you need the information to persist.

显然,只要您需要信息持久化,过期时间就会很长。

hope that helps

希望有帮助