简体   繁体   English

使用原型或纯Java脚本编写文件

[英]Write a file with Prototype or plain Javascript

I know the question has been asked thousands of times, but I'll ask it again: is there a way (even patchy) to write/read a dumb text file with Javascript or Protoype ? 我知道这个问题已经被问过数千次了,但是我会再问一次:是否有一种方法(甚至不完整),可以使用Javascript或Protoype编写/读取一个哑文本文件?

This is only for debug purposes, and is not designed for production. 这仅用于调试目的,不适用于生产环境。 The thing is I need it to work with (at least) both Firefox and IE (preferably under Windows). 问题是我需要它(至少)与Firefox和IE(最好在Windows下)一起使用。

Thanks in advance ! 提前致谢 !

Writing to a file is not possible, you'd have to write a server-side script and make a request to that script. 无法写入文件,您必须编写服务器端脚本并向该脚本发出请求。 Reading is possible if you use an iframe with the text file's location as source, and reading the iframe contents. 如果您使用以文本文件的位置为源的iframe并读取iframe内容,则可以进行读取。

It is possible to read/write to a local file via JavaScript : take a look at TiddlyWIki . 可以读取/写入通过JavaScript本地文件 :看一看的TW (Caveat: only works for local documents.) (注意:仅适用于本地文档。)

I have actually written a Single Page Application (SPA) using twFile , a part of the TiddlyWiki codebase: 我实际上已经使用twFile编写了一个单页应用程序 (SPA),该文件是TiddlyWiki代码库的一部分:

  1. Works in different browsers: (IE, Firefox, Chrome) 适用于不同的浏览器:(IE,Firefox,Chrome)
  2. This code is a little old now. 该代码现在有点旧了。 TiddlyWiki abandoned the jQuery plugin design a while ago. TiddlyWiki不久前放弃了jQuery插件设计。 (Look at the current TiddlyWiki filesystem.js for more a more recent implementation. It's not isolated for you like the twFile plug-in, though). (请查看当前的TiddlyWiki filesystem.js ,以获取更多更新的实现。不过,像twFile插件一样,它并不是孤立的)。
  3. Although written as a jQuery plug-in, I've studied the code and it is almost completely decoupled from jQuery. 尽管是作为jQuery插件编写的,但我已经研究了代码,并且几乎完全与jQuery分离。

Update: I have uploaded a proof-of-concept that accesses a local file via JavaScript. 更新:我已经上传了一个概念证明 ,可以通过JavaScript访问本地文件。

  • Modifying this application to write to a file is trivial. 修改此应用程序以写入文件是微不足道的。
  • I have not tried to get this to work as a file served from a web server, but it should be possible since there are server-side implementations of TiddlyWiki < >. 我没有尝试使它作为Web服务器提供的文件工作,但是由于 TiddlyWiki < >的 服务器端实现 ,因此它应该是可能的

Update: 更新:

So it looks like the server side implementations of TiddlyWiki use a server "adapter" to modify a file stored on the server, similar to Peter's description . 因此,看起来TiddlyWiki的服务器端实现使用服务器“适配器”来修改存储在服务器上的文件,类似于Peter的描述 The pure JavaScript method will probably not work if the page is served from a web server due to cross-domain security limitations. 如果由于跨域安全性限制而从Web服务器提供页面,则纯JavaScript方法可能无法工作。

Javascript in browsers doesn't allow you to write local files, for security reasons . 出于安全原因 ,浏览器中的Javascript不允许您编写本地文件。 This may change with time , but as for now you have to deal with it . 可能会随着时间而改变 ,但是就目前而言,您必须处理它

仅使用服务器端javascript解释器,但这不是在其中运行javascript的典型环境。

What about cookies? 饼干呢? It is accessible via javascript, it is on your client and it is a plain text file. 可通过javascript访问它,它在您的客户端上,并且是纯文本文件。 Only issue is the size of it (4k max if I remember well). 唯一的问题是它的大小(如果我没记错的话,最大为4k)。

What you can do as well is use your browser localStorage / userData / globalStorage (depending on your browser version). 您还可以做的是使用浏览器localStorage / userData / globalStorage(取决于您的浏览器版本)。 It acts like cookies (new webStorage / HTML5 specs) but can handle bigger amounts of data. 它的行为类似于Cookie(新的webStorage / HTML5规范),但可以处理大量数据。 Then, using some add ons (firebug on firefox for instance) you can easily read / copy / past the value and do whatever you have to do with it! 然后,使用一些插件(例如firefox上的萤火虫),您可以轻松读取/复制/超过该值,并执行与该值有关的任何操作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM