使用 jQuery 或 Javascript 编写本地文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2090747/
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
Write local file with jQuery or Javascript
提问by
I am creating a prototype that will be run in a web browser. I would like to store (and retrieve) various info submitted by a user during the use of the prototype. I know I can retrieve a variety of file formats such as xml, json... and I know I can do local storage using the plugin jstore. So I suppose I can do a convoluted solution where I get the first xml load from a starter file and then use local storage after it is manipulated and just check for local storage.
我正在创建一个将在 Web 浏览器中运行的原型。我想存储(和检索)用户在使用原型期间提交的各种信息。我知道我可以检索各种文件格式,例如 xml、json ……并且我知道我可以使用插件 jstore 进行本地存储。所以我想我可以做一个复杂的解决方案,我从启动文件中获取第一个 xml 加载,然后在操作后使用本地存储并检查本地存储。
I do not want to use PHP or ASP or other scripts as I need to be able to share the prototype with senior execs who will not have webservers running and I do not want to install the prototype on the webserver... but perhaps that is the best way to do this as I know javascript can not write to the local file system for security reasons. On the otherhand maybe I am making the combination of getting the initial xml and using the jstore plugin sound harder than it really is.
我不想使用 PHP 或 ASP 或其他脚本,因为我需要能够与不会运行网络服务器的高级执行官共享原型,而且我不想在网络服务器上安装原型......但也许就是这样最好的方法是这样做,因为我知道出于安全原因,javascript 无法写入本地文件系统。另一方面,也许我正在将获取初始 xml 和使用 jstore 插件的组合听起来比实际更难。
回答by Michael Haren
The web platform is specifically designed to prevent sites from writing arbitrary data to client machines. There are a few exceptions to this that you can explore:
Web 平台专门设计用于防止站点将任意数据写入客户端计算机。您可以探索一些例外情况:
- A plugin like Google Gears; or data store in HTML5 (not widely available) (easily lost)
- Cookies (easily lost)
- URL (limited by length, easily lost)
- Files (more conventional albeit clumsy)
- 像 Google Gears 这样的插件;或 HTML5 中的数据存储(未广泛使用)(容易丢失)
- 饼干(容易丢失)
- URL(受长度限制,容易丢失)
- 文件(虽然笨拙但更传统)
The web clearly operates better from a server. If you are hoping to deploy desktop installs, you can deploy a thick application that uses a lot of browser-based UI. That app could easily have access to the local system for files, etc. If a possibility, please comment.
显然,网络在服务器上运行得更好。如果您希望部署桌面安装,您可以部署一个使用大量基于浏览器的 UI 的厚应用程序。该应用程序可以轻松访问本地系统以获取文件等。如果可能,请发表评论。
回答by davidsleeps
If the limitations of using Internet Explorer and all those issues of trust (permission to write to a users file system using javascript is probably only ever going to be possible on an intranet) then you can use the Scripting.fileSystemObject as per this msdn page.
如果使用 Internet Explorer 的限制和所有这些信任问题(使用 javascript 写入用户文件系统的权限可能只能在 Intranet 上实现),那么您可以使用 Scripting.fileSystemObject 根据此 msdn 页面。
回答by Shashank
I agree with all the opinion that it is difficult to store data in client side using JavaScript. If we have to create a stand alone application then adobe air seems to be promising. As per specs and features stated by adobe we can use JavaScript and html skill to create an application also it allows you to read /write to a local storage in file or database.
我同意所有的观点,即使用 JavaScript 在客户端存储数据很困难。如果我们必须创建一个独立的应用程序,那么 adobe air 似乎很有希望。根据 adobe 所述的规格和功能,我们可以使用 JavaScript 和 html 技能来创建应用程序,它还允许您读取/写入文件或数据库中的本地存储。