简体   繁体   English

在浏览器中写入没有外部JS库(纯Javascript)的文件?

[英]Write to a file without external JS libraries (pure Javascript) in a browser ?

Is it possible to write a string to a file (create the file if it doesn't exist) without any external Javascript libraries , purely with Javascript code ? 是否可以在没有任何外部Javascript库的情况下,仅使用Javascript代码将字符串写入文件(如果不存在,则创建文件)?

I'm trying to implement a client side code that whenever it reaches a page (.ASPX) it writes few strings into a file (let's say output.txt ) and that's it . 我正在尝试实现一个客户端代码,只要它到达页面(.ASPX),它就会向文件中写入几个字符串(比如output.txt ),仅此而已。

From everything I see over the internet , JavaScript is not allowed to write anything to the hard drive . 从我在互联网上看到的一切来看,不允许JavaScript向硬盘驱动器写入任何内容。

Is that so ? 是这样吗 ? no workarounds ? 没有解决方法?

As said in the comments, what you sound to try to achieve is intentionally very restricted for obvious security reasons of the visitor's machine. 如评论中所述,出于访问者机器的明显安全原因,您试图实现的目标非常受限制。

Now "workarounds" could exist, depending on the actual objective you are trying to achieve (but which you did not mention). 现在,可能会存在“变通办法”,具体取决于您要实现的实际目标(但您没有提到)。

  • Cookies to store simple data (like your strings) in the visitor's computer (so strictly speaking, it does the job you ask for, but you have no control on which text file is used). Cookie,用于在访问者的计算机中存储简单的数据(例如字符串)(严格来说,它可以完成您所要求的工作,但是您无法控制使用哪个文本文件)。
  • Use a browser extension, which would have the permission to write on the visitor's filesystem. 使用浏览器扩展程序,该扩展程序有权在访问者的文件系统上进行写操作。 Applicable if you can get visitors to install such extension (eg if you are IT and can request employees installing specific software). 如果您可以让访问者安装此类扩展程序(例如,如果您是IT并可以要求员工安装特定软件),则适用。
  • localStorage as proposed in IanKenney's comment, if you just want some persistent data. 如果您只需要一些持久性数据,请按照IanKenney的评论中的建议使用localStorage。

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

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